xref: /openbmc/bmcweb/features/redfish/lib/managers.hpp (revision 1214b7e7d921e331fb1480c7e5d579ffa5811cda)
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>
27*1214b7e7SGunnar Mills 
28*1214b7e7SGunnar Mills #include <memory>
29*1214b7e7SGunnar Mills #include <sstream>
30abf2add6SEd Tanous #include <variant>
315b4aa86bSJames Feist 
321abe55efSEd Tanous namespace redfish
331abe55efSEd Tanous {
34ed5befbdSJennifer Lee 
35ed5befbdSJennifer Lee /**
362a5c4407SGunnar Mills  * Function reboots the BMC.
372a5c4407SGunnar Mills  *
382a5c4407SGunnar Mills  * @param[in] asyncResp - Shared pointer for completing asynchronous calls
39ed5befbdSJennifer Lee  */
402a5c4407SGunnar Mills void doBMCGracefulRestart(std::shared_ptr<AsyncResp> asyncResp)
41ed5befbdSJennifer Lee {
42ed5befbdSJennifer Lee     const char* processName = "xyz.openbmc_project.State.BMC";
43ed5befbdSJennifer Lee     const char* objectPath = "/xyz/openbmc_project/state/bmc0";
44ed5befbdSJennifer Lee     const char* interfaceName = "xyz.openbmc_project.State.BMC";
45ed5befbdSJennifer Lee     const std::string& propertyValue =
46ed5befbdSJennifer Lee         "xyz.openbmc_project.State.BMC.Transition.Reboot";
47ed5befbdSJennifer Lee     const char* destProperty = "RequestedBMCTransition";
48ed5befbdSJennifer Lee 
49ed5befbdSJennifer Lee     // Create the D-Bus variant for D-Bus call.
50ed5befbdSJennifer Lee     VariantType dbusPropertyValue(propertyValue);
51ed5befbdSJennifer Lee 
52ed5befbdSJennifer Lee     crow::connections::systemBus->async_method_call(
53ed5befbdSJennifer Lee         [asyncResp](const boost::system::error_code ec) {
54ed5befbdSJennifer Lee             // Use "Set" method to set the property value.
55ed5befbdSJennifer Lee             if (ec)
56ed5befbdSJennifer Lee             {
572a5c4407SGunnar Mills                 BMCWEB_LOG_DEBUG << "[Set] Bad D-Bus request error: " << ec;
58ed5befbdSJennifer Lee                 messages::internalError(asyncResp->res);
59ed5befbdSJennifer Lee                 return;
60ed5befbdSJennifer Lee             }
61ed5befbdSJennifer Lee 
62ed5befbdSJennifer Lee             messages::success(asyncResp->res);
63ed5befbdSJennifer Lee         },
64ed5befbdSJennifer Lee         processName, objectPath, "org.freedesktop.DBus.Properties", "Set",
65ed5befbdSJennifer Lee         interfaceName, destProperty, dbusPropertyValue);
66ed5befbdSJennifer Lee }
672a5c4407SGunnar Mills 
682a5c4407SGunnar Mills /**
692a5c4407SGunnar Mills  * ManagerResetAction class supports the POST method for the Reset (reboot)
702a5c4407SGunnar Mills  * action.
712a5c4407SGunnar Mills  */
722a5c4407SGunnar Mills class ManagerResetAction : public Node
732a5c4407SGunnar Mills {
742a5c4407SGunnar Mills   public:
752a5c4407SGunnar Mills     ManagerResetAction(CrowApp& app) :
762a5c4407SGunnar Mills         Node(app, "/redfish/v1/Managers/bmc/Actions/Manager.Reset/")
772a5c4407SGunnar Mills     {
782a5c4407SGunnar Mills         entityPrivileges = {
792a5c4407SGunnar Mills             {boost::beast::http::verb::post, {{"ConfigureManager"}}}};
802a5c4407SGunnar Mills     }
812a5c4407SGunnar Mills 
822a5c4407SGunnar Mills   private:
832a5c4407SGunnar Mills     /**
842a5c4407SGunnar Mills      * Function handles POST method request.
852a5c4407SGunnar Mills      * Analyzes POST body before sending Reset (Reboot) request data to D-Bus.
862a5c4407SGunnar Mills      * OpenBMC only supports ResetType "GracefulRestart".
872a5c4407SGunnar Mills      */
882a5c4407SGunnar Mills     void doPost(crow::Response& res, const crow::Request& req,
892a5c4407SGunnar Mills                 const std::vector<std::string>& params) override
902a5c4407SGunnar Mills     {
912a5c4407SGunnar Mills         BMCWEB_LOG_DEBUG << "Post Manager Reset.";
922a5c4407SGunnar Mills 
932a5c4407SGunnar Mills         std::string resetType;
942a5c4407SGunnar Mills         auto asyncResp = std::make_shared<AsyncResp>(res);
952a5c4407SGunnar Mills 
962a5c4407SGunnar Mills         if (!json_util::readJson(req, asyncResp->res, "ResetType", resetType))
972a5c4407SGunnar Mills         {
982a5c4407SGunnar Mills             return;
992a5c4407SGunnar Mills         }
1002a5c4407SGunnar Mills 
1012a5c4407SGunnar Mills         if (resetType != "GracefulRestart")
1022a5c4407SGunnar Mills         {
1032a5c4407SGunnar Mills             BMCWEB_LOG_DEBUG << "Invalid property value for ResetType: "
1042a5c4407SGunnar Mills                              << resetType;
1052a5c4407SGunnar Mills             messages::actionParameterNotSupported(asyncResp->res, resetType,
1062a5c4407SGunnar Mills                                                   "ResetType");
1072a5c4407SGunnar Mills 
1082a5c4407SGunnar Mills             return;
1092a5c4407SGunnar Mills         }
1102a5c4407SGunnar Mills         doBMCGracefulRestart(asyncResp);
1112a5c4407SGunnar Mills     }
112ed5befbdSJennifer Lee };
113ed5befbdSJennifer Lee 
1143e40fc74SGunnar Mills /**
1153e40fc74SGunnar Mills  * ManagerResetToDefaultsAction class supports POST method for factory reset
1163e40fc74SGunnar Mills  * action.
1173e40fc74SGunnar Mills  */
1183e40fc74SGunnar Mills class ManagerResetToDefaultsAction : public Node
1193e40fc74SGunnar Mills {
1203e40fc74SGunnar Mills   public:
1213e40fc74SGunnar Mills     ManagerResetToDefaultsAction(CrowApp& app) :
1223e40fc74SGunnar Mills         Node(app, "/redfish/v1/Managers/bmc/Actions/Manager.ResetToDefaults/")
1233e40fc74SGunnar Mills     {
1243e40fc74SGunnar Mills         entityPrivileges = {
1253e40fc74SGunnar Mills             {boost::beast::http::verb::post, {{"ConfigureManager"}}}};
1263e40fc74SGunnar Mills     }
1273e40fc74SGunnar Mills 
1283e40fc74SGunnar Mills   private:
1293e40fc74SGunnar Mills     /**
1303e40fc74SGunnar Mills      * Function handles ResetToDefaults POST method request.
1313e40fc74SGunnar Mills      *
1323e40fc74SGunnar Mills      * Analyzes POST body message and factory resets BMC by calling
1333e40fc74SGunnar Mills      * BMC code updater factory reset followed by a BMC reboot.
1343e40fc74SGunnar Mills      *
1353e40fc74SGunnar Mills      * BMC code updater factory reset wipes the whole BMC read-write
1363e40fc74SGunnar Mills      * filesystem which includes things like the network settings.
1373e40fc74SGunnar Mills      *
1383e40fc74SGunnar Mills      * OpenBMC only supports ResetToDefaultsType "ResetAll".
1393e40fc74SGunnar Mills      */
1403e40fc74SGunnar Mills     void doPost(crow::Response& res, const crow::Request& req,
1413e40fc74SGunnar Mills                 const std::vector<std::string>& params) override
1423e40fc74SGunnar Mills     {
1433e40fc74SGunnar Mills         BMCWEB_LOG_DEBUG << "Post ResetToDefaults.";
1443e40fc74SGunnar Mills 
1453e40fc74SGunnar Mills         std::string resetType;
1463e40fc74SGunnar Mills         auto asyncResp = std::make_shared<AsyncResp>(res);
1473e40fc74SGunnar Mills 
1483e40fc74SGunnar Mills         if (!json_util::readJson(req, asyncResp->res, "ResetToDefaultsType",
1493e40fc74SGunnar Mills                                  resetType))
1503e40fc74SGunnar Mills         {
1513e40fc74SGunnar Mills             BMCWEB_LOG_DEBUG << "Missing property ResetToDefaultsType.";
1523e40fc74SGunnar Mills 
1533e40fc74SGunnar Mills             messages::actionParameterMissing(asyncResp->res, "ResetToDefaults",
1543e40fc74SGunnar Mills                                              "ResetToDefaultsType");
1553e40fc74SGunnar Mills             return;
1563e40fc74SGunnar Mills         }
1573e40fc74SGunnar Mills 
1583e40fc74SGunnar Mills         if (resetType != "ResetAll")
1593e40fc74SGunnar Mills         {
1603e40fc74SGunnar Mills             BMCWEB_LOG_DEBUG << "Invalid property value for "
1613e40fc74SGunnar Mills                                 "ResetToDefaultsType: "
1623e40fc74SGunnar Mills                              << resetType;
1633e40fc74SGunnar Mills             messages::actionParameterNotSupported(asyncResp->res, resetType,
1643e40fc74SGunnar Mills                                                   "ResetToDefaultsType");
1653e40fc74SGunnar Mills             return;
1663e40fc74SGunnar Mills         }
1673e40fc74SGunnar Mills 
1683e40fc74SGunnar Mills         crow::connections::systemBus->async_method_call(
1693e40fc74SGunnar Mills             [asyncResp](const boost::system::error_code ec) {
1703e40fc74SGunnar Mills                 if (ec)
1713e40fc74SGunnar Mills                 {
1723e40fc74SGunnar Mills                     BMCWEB_LOG_DEBUG << "Failed to ResetToDefaults: " << ec;
1733e40fc74SGunnar Mills                     messages::internalError(asyncResp->res);
1743e40fc74SGunnar Mills                     return;
1753e40fc74SGunnar Mills                 }
1763e40fc74SGunnar Mills                 // Factory Reset doesn't actually happen until a reboot
1773e40fc74SGunnar Mills                 // Can't erase what the BMC is running on
1783e40fc74SGunnar Mills                 doBMCGracefulRestart(asyncResp);
1793e40fc74SGunnar Mills             },
1803e40fc74SGunnar Mills             "xyz.openbmc_project.Software.BMC.Updater",
1813e40fc74SGunnar Mills             "/xyz/openbmc_project/software",
1823e40fc74SGunnar Mills             "xyz.openbmc_project.Common.FactoryReset", "Reset");
1833e40fc74SGunnar Mills     }
1843e40fc74SGunnar Mills };
1853e40fc74SGunnar Mills 
1865b4aa86bSJames Feist static constexpr const char* objectManagerIface =
1875b4aa86bSJames Feist     "org.freedesktop.DBus.ObjectManager";
1885b4aa86bSJames Feist static constexpr const char* pidConfigurationIface =
1895b4aa86bSJames Feist     "xyz.openbmc_project.Configuration.Pid";
1905b4aa86bSJames Feist static constexpr const char* pidZoneConfigurationIface =
1915b4aa86bSJames Feist     "xyz.openbmc_project.Configuration.Pid.Zone";
192b7a08d04SJames Feist static constexpr const char* stepwiseConfigurationIface =
193b7a08d04SJames Feist     "xyz.openbmc_project.Configuration.Stepwise";
19473df0db0SJames Feist static constexpr const char* thermalModeIface =
19573df0db0SJames Feist     "xyz.openbmc_project.Control.ThermalMode";
1969c310685SBorawski.Lukasz 
1975b4aa86bSJames Feist static void asyncPopulatePid(const std::string& connection,
1985b4aa86bSJames Feist                              const std::string& path,
19973df0db0SJames Feist                              const std::string& currentProfile,
20073df0db0SJames Feist                              const std::vector<std::string>& supportedProfiles,
2015b4aa86bSJames Feist                              std::shared_ptr<AsyncResp> asyncResp)
2025b4aa86bSJames Feist {
2035b4aa86bSJames Feist 
2045b4aa86bSJames Feist     crow::connections::systemBus->async_method_call(
20573df0db0SJames Feist         [asyncResp, currentProfile, supportedProfiles](
20673df0db0SJames Feist             const boost::system::error_code ec,
2075b4aa86bSJames Feist             const dbus::utility::ManagedObjectType& managedObj) {
2085b4aa86bSJames Feist             if (ec)
2095b4aa86bSJames Feist             {
2105b4aa86bSJames Feist                 BMCWEB_LOG_ERROR << ec;
2115b4aa86bSJames Feist                 asyncResp->res.jsonValue.clear();
212f12894f8SJason M. Bills                 messages::internalError(asyncResp->res);
2135b4aa86bSJames Feist                 return;
2145b4aa86bSJames Feist             }
2155b4aa86bSJames Feist             nlohmann::json& configRoot =
2165b4aa86bSJames Feist                 asyncResp->res.jsonValue["Oem"]["OpenBmc"]["Fan"];
2175b4aa86bSJames Feist             nlohmann::json& fans = configRoot["FanControllers"];
2185b4aa86bSJames Feist             fans["@odata.type"] = "#OemManager.FanControllers";
2195b4aa86bSJames Feist             fans["@odata.id"] = "/redfish/v1/Managers/bmc#/Oem/OpenBmc/"
2205b4aa86bSJames Feist                                 "Fan/FanControllers";
2215b4aa86bSJames Feist 
2225b4aa86bSJames Feist             nlohmann::json& pids = configRoot["PidControllers"];
2235b4aa86bSJames Feist             pids["@odata.type"] = "#OemManager.PidControllers";
2245b4aa86bSJames Feist             pids["@odata.id"] =
2255b4aa86bSJames Feist                 "/redfish/v1/Managers/bmc#/Oem/OpenBmc/Fan/PidControllers";
2265b4aa86bSJames Feist 
227b7a08d04SJames Feist             nlohmann::json& stepwise = configRoot["StepwiseControllers"];
228b7a08d04SJames Feist             stepwise["@odata.type"] = "#OemManager.StepwiseControllers";
229b7a08d04SJames Feist             stepwise["@odata.id"] =
230b7a08d04SJames Feist                 "/redfish/v1/Managers/bmc#/Oem/OpenBmc/Fan/StepwiseControllers";
231b7a08d04SJames Feist 
2325b4aa86bSJames Feist             nlohmann::json& zones = configRoot["FanZones"];
2335b4aa86bSJames Feist             zones["@odata.id"] =
2345b4aa86bSJames Feist                 "/redfish/v1/Managers/bmc#/Oem/OpenBmc/Fan/FanZones";
2355b4aa86bSJames Feist             zones["@odata.type"] = "#OemManager.FanZones";
2365b4aa86bSJames Feist             configRoot["@odata.id"] =
2375b4aa86bSJames Feist                 "/redfish/v1/Managers/bmc#/Oem/OpenBmc/Fan";
2385b4aa86bSJames Feist             configRoot["@odata.type"] = "#OemManager.Fan";
23973df0db0SJames Feist             configRoot["Profile@Redfish.AllowableValues"] = supportedProfiles;
24073df0db0SJames Feist 
24173df0db0SJames Feist             if (!currentProfile.empty())
24273df0db0SJames Feist             {
24373df0db0SJames Feist                 configRoot["Profile"] = currentProfile;
24473df0db0SJames Feist             }
24573df0db0SJames Feist             BMCWEB_LOG_ERROR << "profile = " << currentProfile << " !";
2465b4aa86bSJames Feist 
2475b4aa86bSJames Feist             for (const auto& pathPair : managedObj)
2485b4aa86bSJames Feist             {
2495b4aa86bSJames Feist                 for (const auto& intfPair : pathPair.second)
2505b4aa86bSJames Feist                 {
2515b4aa86bSJames Feist                     if (intfPair.first != pidConfigurationIface &&
252b7a08d04SJames Feist                         intfPair.first != pidZoneConfigurationIface &&
253b7a08d04SJames Feist                         intfPair.first != stepwiseConfigurationIface)
2545b4aa86bSJames Feist                     {
2555b4aa86bSJames Feist                         continue;
2565b4aa86bSJames Feist                     }
2575b4aa86bSJames Feist                     auto findName = intfPair.second.find("Name");
2585b4aa86bSJames Feist                     if (findName == intfPair.second.end())
2595b4aa86bSJames Feist                     {
2605b4aa86bSJames Feist                         BMCWEB_LOG_ERROR << "Pid Field missing Name";
261a08b46ccSJason M. Bills                         messages::internalError(asyncResp->res);
2625b4aa86bSJames Feist                         return;
2635b4aa86bSJames Feist                     }
26473df0db0SJames Feist 
2655b4aa86bSJames Feist                     const std::string* namePtr =
266abf2add6SEd Tanous                         std::get_if<std::string>(&findName->second);
2675b4aa86bSJames Feist                     if (namePtr == nullptr)
2685b4aa86bSJames Feist                     {
2695b4aa86bSJames Feist                         BMCWEB_LOG_ERROR << "Pid Name Field illegal";
270b7a08d04SJames Feist                         messages::internalError(asyncResp->res);
2715b4aa86bSJames Feist                         return;
2725b4aa86bSJames Feist                     }
2735b4aa86bSJames Feist                     std::string name = *namePtr;
2745b4aa86bSJames Feist                     dbus::utility::escapePathForDbus(name);
27573df0db0SJames Feist 
27673df0db0SJames Feist                     auto findProfiles = intfPair.second.find("Profiles");
27773df0db0SJames Feist                     if (findProfiles != intfPair.second.end())
27873df0db0SJames Feist                     {
27973df0db0SJames Feist                         const std::vector<std::string>* profiles =
28073df0db0SJames Feist                             std::get_if<std::vector<std::string>>(
28173df0db0SJames Feist                                 &findProfiles->second);
28273df0db0SJames Feist                         if (profiles == nullptr)
28373df0db0SJames Feist                         {
28473df0db0SJames Feist                             BMCWEB_LOG_ERROR << "Pid Profiles Field illegal";
28573df0db0SJames Feist                             messages::internalError(asyncResp->res);
28673df0db0SJames Feist                             return;
28773df0db0SJames Feist                         }
28873df0db0SJames Feist                         if (std::find(profiles->begin(), profiles->end(),
28973df0db0SJames Feist                                       currentProfile) == profiles->end())
29073df0db0SJames Feist                         {
29173df0db0SJames Feist                             BMCWEB_LOG_INFO
29273df0db0SJames Feist                                 << name << " not supported in current profile";
29373df0db0SJames Feist                             continue;
29473df0db0SJames Feist                         }
29573df0db0SJames Feist                     }
296b7a08d04SJames Feist                     nlohmann::json* config = nullptr;
297c33a90ecSJames Feist 
298c33a90ecSJames Feist                     const std::string* classPtr = nullptr;
299c33a90ecSJames Feist                     auto findClass = intfPair.second.find("Class");
300c33a90ecSJames Feist                     if (findClass != intfPair.second.end())
301c33a90ecSJames Feist                     {
302c33a90ecSJames Feist                         classPtr = std::get_if<std::string>(&findClass->second);
303c33a90ecSJames Feist                     }
304c33a90ecSJames Feist 
3055b4aa86bSJames Feist                     if (intfPair.first == pidZoneConfigurationIface)
3065b4aa86bSJames Feist                     {
3075b4aa86bSJames Feist                         std::string chassis;
3085b4aa86bSJames Feist                         if (!dbus::utility::getNthStringFromPath(
3095b4aa86bSJames Feist                                 pathPair.first.str, 5, chassis))
3105b4aa86bSJames Feist                         {
3115b4aa86bSJames Feist                             chassis = "#IllegalValue";
3125b4aa86bSJames Feist                         }
3135b4aa86bSJames Feist                         nlohmann::json& zone = zones[name];
3145b4aa86bSJames Feist                         zone["Chassis"] = {
3155b4aa86bSJames Feist                             {"@odata.id", "/redfish/v1/Chassis/" + chassis}};
3165b4aa86bSJames Feist                         zone["@odata.id"] = "/redfish/v1/Managers/bmc#/Oem/"
3175b4aa86bSJames Feist                                             "OpenBmc/Fan/FanZones/" +
3185b4aa86bSJames Feist                                             name;
3195b4aa86bSJames Feist                         zone["@odata.type"] = "#OemManager.FanZone";
320b7a08d04SJames Feist                         config = &zone;
3215b4aa86bSJames Feist                     }
3225b4aa86bSJames Feist 
323b7a08d04SJames Feist                     else if (intfPair.first == stepwiseConfigurationIface)
3245b4aa86bSJames Feist                     {
325c33a90ecSJames Feist                         if (classPtr == nullptr)
326c33a90ecSJames Feist                         {
327c33a90ecSJames Feist                             BMCWEB_LOG_ERROR << "Pid Class Field illegal";
328c33a90ecSJames Feist                             messages::internalError(asyncResp->res);
329c33a90ecSJames Feist                             return;
330c33a90ecSJames Feist                         }
331c33a90ecSJames Feist 
332b7a08d04SJames Feist                         nlohmann::json& controller = stepwise[name];
333b7a08d04SJames Feist                         config = &controller;
3345b4aa86bSJames Feist 
335b7a08d04SJames Feist                         controller["@odata.id"] =
336b7a08d04SJames Feist                             "/redfish/v1/Managers/bmc#/Oem/"
337b7a08d04SJames Feist                             "OpenBmc/Fan/StepwiseControllers/" +
338271584abSEd Tanous                             name;
339b7a08d04SJames Feist                         controller["@odata.type"] =
340b7a08d04SJames Feist                             "#OemManager.StepwiseController";
341b7a08d04SJames Feist 
342c33a90ecSJames Feist                         controller["Direction"] = *classPtr;
3435b4aa86bSJames Feist                     }
3445b4aa86bSJames Feist 
3455b4aa86bSJames Feist                     // pid and fans are off the same configuration
346b7a08d04SJames Feist                     else if (intfPair.first == pidConfigurationIface)
3475b4aa86bSJames Feist                     {
348c33a90ecSJames Feist 
3495b4aa86bSJames Feist                         if (classPtr == nullptr)
3505b4aa86bSJames Feist                         {
3515b4aa86bSJames Feist                             BMCWEB_LOG_ERROR << "Pid Class Field illegal";
352a08b46ccSJason M. Bills                             messages::internalError(asyncResp->res);
3535b4aa86bSJames Feist                             return;
3545b4aa86bSJames Feist                         }
3555b4aa86bSJames Feist                         bool isFan = *classPtr == "fan";
3565b4aa86bSJames Feist                         nlohmann::json& element =
3575b4aa86bSJames Feist                             isFan ? fans[name] : pids[name];
358b7a08d04SJames Feist                         config = &element;
3595b4aa86bSJames Feist                         if (isFan)
3605b4aa86bSJames Feist                         {
3615b4aa86bSJames Feist                             element["@odata.id"] =
3625b4aa86bSJames Feist                                 "/redfish/v1/Managers/bmc#/Oem/"
3635b4aa86bSJames Feist                                 "OpenBmc/Fan/FanControllers/" +
364271584abSEd Tanous                                 name;
3655b4aa86bSJames Feist                             element["@odata.type"] =
3665b4aa86bSJames Feist                                 "#OemManager.FanController";
3675b4aa86bSJames Feist                         }
3685b4aa86bSJames Feist                         else
3695b4aa86bSJames Feist                         {
3705b4aa86bSJames Feist                             element["@odata.id"] =
3715b4aa86bSJames Feist                                 "/redfish/v1/Managers/bmc#/Oem/"
3725b4aa86bSJames Feist                                 "OpenBmc/Fan/PidControllers/" +
373271584abSEd Tanous                                 name;
3745b4aa86bSJames Feist                             element["@odata.type"] =
3755b4aa86bSJames Feist                                 "#OemManager.PidController";
3765b4aa86bSJames Feist                         }
377b7a08d04SJames Feist                     }
378b7a08d04SJames Feist                     else
379b7a08d04SJames Feist                     {
380b7a08d04SJames Feist                         BMCWEB_LOG_ERROR << "Unexpected configuration";
381b7a08d04SJames Feist                         messages::internalError(asyncResp->res);
382b7a08d04SJames Feist                         return;
383b7a08d04SJames Feist                     }
384b7a08d04SJames Feist 
385b7a08d04SJames Feist                     // used for making maps out of 2 vectors
386b7a08d04SJames Feist                     const std::vector<double>* keys = nullptr;
387b7a08d04SJames Feist                     const std::vector<double>* values = nullptr;
388b7a08d04SJames Feist 
389b7a08d04SJames Feist                     for (const auto& propertyPair : intfPair.second)
390b7a08d04SJames Feist                     {
391b7a08d04SJames Feist                         if (propertyPair.first == "Type" ||
392b7a08d04SJames Feist                             propertyPair.first == "Class" ||
393b7a08d04SJames Feist                             propertyPair.first == "Name")
394b7a08d04SJames Feist                         {
395b7a08d04SJames Feist                             continue;
396b7a08d04SJames Feist                         }
397b7a08d04SJames Feist 
398b7a08d04SJames Feist                         // zones
399b7a08d04SJames Feist                         if (intfPair.first == pidZoneConfigurationIface)
400b7a08d04SJames Feist                         {
401b7a08d04SJames Feist                             const double* ptr =
402abf2add6SEd Tanous                                 std::get_if<double>(&propertyPair.second);
403b7a08d04SJames Feist                             if (ptr == nullptr)
404b7a08d04SJames Feist                             {
405b7a08d04SJames Feist                                 BMCWEB_LOG_ERROR << "Field Illegal "
406b7a08d04SJames Feist                                                  << propertyPair.first;
407b7a08d04SJames Feist                                 messages::internalError(asyncResp->res);
408b7a08d04SJames Feist                                 return;
409b7a08d04SJames Feist                             }
410b7a08d04SJames Feist                             (*config)[propertyPair.first] = *ptr;
411b7a08d04SJames Feist                         }
412b7a08d04SJames Feist 
413b7a08d04SJames Feist                         if (intfPair.first == stepwiseConfigurationIface)
414b7a08d04SJames Feist                         {
415b7a08d04SJames Feist                             if (propertyPair.first == "Reading" ||
416b7a08d04SJames Feist                                 propertyPair.first == "Output")
417b7a08d04SJames Feist                             {
418b7a08d04SJames Feist                                 const std::vector<double>* ptr =
419abf2add6SEd Tanous                                     std::get_if<std::vector<double>>(
420b7a08d04SJames Feist                                         &propertyPair.second);
421b7a08d04SJames Feist 
422b7a08d04SJames Feist                                 if (ptr == nullptr)
423b7a08d04SJames Feist                                 {
424b7a08d04SJames Feist                                     BMCWEB_LOG_ERROR << "Field Illegal "
425b7a08d04SJames Feist                                                      << propertyPair.first;
426b7a08d04SJames Feist                                     messages::internalError(asyncResp->res);
427b7a08d04SJames Feist                                     return;
428b7a08d04SJames Feist                                 }
429b7a08d04SJames Feist 
430b7a08d04SJames Feist                                 if (propertyPair.first == "Reading")
431b7a08d04SJames Feist                                 {
432b7a08d04SJames Feist                                     keys = ptr;
433b7a08d04SJames Feist                                 }
434b7a08d04SJames Feist                                 else
435b7a08d04SJames Feist                                 {
436b7a08d04SJames Feist                                     values = ptr;
437b7a08d04SJames Feist                                 }
438b7a08d04SJames Feist                                 if (keys && values)
439b7a08d04SJames Feist                                 {
440b7a08d04SJames Feist                                     if (keys->size() != values->size())
441b7a08d04SJames Feist                                     {
442b7a08d04SJames Feist                                         BMCWEB_LOG_ERROR
443b7a08d04SJames Feist                                             << "Reading and Output size don't "
444b7a08d04SJames Feist                                                "match ";
445b7a08d04SJames Feist                                         messages::internalError(asyncResp->res);
446b7a08d04SJames Feist                                         return;
447b7a08d04SJames Feist                                     }
448b7a08d04SJames Feist                                     nlohmann::json& steps = (*config)["Steps"];
449b7a08d04SJames Feist                                     steps = nlohmann::json::array();
450b7a08d04SJames Feist                                     for (size_t ii = 0; ii < keys->size(); ii++)
451b7a08d04SJames Feist                                     {
452b7a08d04SJames Feist                                         steps.push_back(
453b7a08d04SJames Feist                                             {{"Target", (*keys)[ii]},
454b7a08d04SJames Feist                                              {"Output", (*values)[ii]}});
455b7a08d04SJames Feist                                     }
456b7a08d04SJames Feist                                 }
457b7a08d04SJames Feist                             }
458b7a08d04SJames Feist                             if (propertyPair.first == "NegativeHysteresis" ||
459b7a08d04SJames Feist                                 propertyPair.first == "PositiveHysteresis")
460b7a08d04SJames Feist                             {
461b7a08d04SJames Feist                                 const double* ptr =
462abf2add6SEd Tanous                                     std::get_if<double>(&propertyPair.second);
463b7a08d04SJames Feist                                 if (ptr == nullptr)
464b7a08d04SJames Feist                                 {
465b7a08d04SJames Feist                                     BMCWEB_LOG_ERROR << "Field Illegal "
466b7a08d04SJames Feist                                                      << propertyPair.first;
467b7a08d04SJames Feist                                     messages::internalError(asyncResp->res);
468b7a08d04SJames Feist                                     return;
469b7a08d04SJames Feist                                 }
470b7a08d04SJames Feist                                 (*config)[propertyPair.first] = *ptr;
471b7a08d04SJames Feist                             }
472b7a08d04SJames Feist                         }
473b7a08d04SJames Feist 
474b7a08d04SJames Feist                         // pid and fans are off the same configuration
475b7a08d04SJames Feist                         if (intfPair.first == pidConfigurationIface ||
476b7a08d04SJames Feist                             intfPair.first == stepwiseConfigurationIface)
477b7a08d04SJames Feist                         {
4785b4aa86bSJames Feist 
4795b4aa86bSJames Feist                             if (propertyPair.first == "Zones")
4805b4aa86bSJames Feist                             {
4815b4aa86bSJames Feist                                 const std::vector<std::string>* inputs =
482abf2add6SEd Tanous                                     std::get_if<std::vector<std::string>>(
4831b6b96c5SEd Tanous                                         &propertyPair.second);
4845b4aa86bSJames Feist 
4855b4aa86bSJames Feist                                 if (inputs == nullptr)
4865b4aa86bSJames Feist                                 {
4875b4aa86bSJames Feist                                     BMCWEB_LOG_ERROR
4885b4aa86bSJames Feist                                         << "Zones Pid Field Illegal";
489a08b46ccSJason M. Bills                                     messages::internalError(asyncResp->res);
4905b4aa86bSJames Feist                                     return;
4915b4aa86bSJames Feist                                 }
492b7a08d04SJames Feist                                 auto& data = (*config)[propertyPair.first];
4935b4aa86bSJames Feist                                 data = nlohmann::json::array();
4945b4aa86bSJames Feist                                 for (std::string itemCopy : *inputs)
4955b4aa86bSJames Feist                                 {
4965b4aa86bSJames Feist                                     dbus::utility::escapePathForDbus(itemCopy);
4975b4aa86bSJames Feist                                     data.push_back(
4985b4aa86bSJames Feist                                         {{"@odata.id",
4995b4aa86bSJames Feist                                           "/redfish/v1/Managers/bmc#/Oem/"
5005b4aa86bSJames Feist                                           "OpenBmc/Fan/FanZones/" +
5015b4aa86bSJames Feist                                               itemCopy}});
5025b4aa86bSJames Feist                                 }
5035b4aa86bSJames Feist                             }
5045b4aa86bSJames Feist                             // todo(james): may never happen, but this
5055b4aa86bSJames Feist                             // assumes configuration data referenced in the
5065b4aa86bSJames Feist                             // PID config is provided by the same daemon, we
5075b4aa86bSJames Feist                             // could add another loop to cover all cases,
5085b4aa86bSJames Feist                             // but I'm okay kicking this can down the road a
5095b4aa86bSJames Feist                             // bit
5105b4aa86bSJames Feist 
5115b4aa86bSJames Feist                             else if (propertyPair.first == "Inputs" ||
5125b4aa86bSJames Feist                                      propertyPair.first == "Outputs")
5135b4aa86bSJames Feist                             {
514b7a08d04SJames Feist                                 auto& data = (*config)[propertyPair.first];
5155b4aa86bSJames Feist                                 const std::vector<std::string>* inputs =
516abf2add6SEd Tanous                                     std::get_if<std::vector<std::string>>(
5171b6b96c5SEd Tanous                                         &propertyPair.second);
5185b4aa86bSJames Feist 
5195b4aa86bSJames Feist                                 if (inputs == nullptr)
5205b4aa86bSJames Feist                                 {
5215b4aa86bSJames Feist                                     BMCWEB_LOG_ERROR << "Field Illegal "
5225b4aa86bSJames Feist                                                      << propertyPair.first;
523f12894f8SJason M. Bills                                     messages::internalError(asyncResp->res);
5245b4aa86bSJames Feist                                     return;
5255b4aa86bSJames Feist                                 }
5265b4aa86bSJames Feist                                 data = *inputs;
527b943aaefSJames Feist                             }
528b943aaefSJames Feist                             else if (propertyPair.first == "SetPointOffset")
529b943aaefSJames Feist                             {
530b943aaefSJames Feist                                 const std::string* ptr =
531b943aaefSJames Feist                                     std::get_if<std::string>(
532b943aaefSJames Feist                                         &propertyPair.second);
533b943aaefSJames Feist 
534b943aaefSJames Feist                                 if (ptr == nullptr)
535b943aaefSJames Feist                                 {
536b943aaefSJames Feist                                     BMCWEB_LOG_ERROR << "Field Illegal "
537b943aaefSJames Feist                                                      << propertyPair.first;
538b943aaefSJames Feist                                     messages::internalError(asyncResp->res);
539b943aaefSJames Feist                                     return;
540b943aaefSJames Feist                                 }
541b943aaefSJames Feist                                 // translate from dbus to redfish
542b943aaefSJames Feist                                 if (*ptr == "WarningHigh")
543b943aaefSJames Feist                                 {
544b943aaefSJames Feist                                     (*config)["SetPointOffset"] =
545b943aaefSJames Feist                                         "UpperThresholdNonCritical";
546b943aaefSJames Feist                                 }
547b943aaefSJames Feist                                 else if (*ptr == "WarningLow")
548b943aaefSJames Feist                                 {
549b943aaefSJames Feist                                     (*config)["SetPointOffset"] =
550b943aaefSJames Feist                                         "LowerThresholdNonCritical";
551b943aaefSJames Feist                                 }
552b943aaefSJames Feist                                 else if (*ptr == "CriticalHigh")
553b943aaefSJames Feist                                 {
554b943aaefSJames Feist                                     (*config)["SetPointOffset"] =
555b943aaefSJames Feist                                         "UpperThresholdCritical";
556b943aaefSJames Feist                                 }
557b943aaefSJames Feist                                 else if (*ptr == "CriticalLow")
558b943aaefSJames Feist                                 {
559b943aaefSJames Feist                                     (*config)["SetPointOffset"] =
560b943aaefSJames Feist                                         "LowerThresholdCritical";
561b943aaefSJames Feist                                 }
562b943aaefSJames Feist                                 else
563b943aaefSJames Feist                                 {
564b943aaefSJames Feist                                     BMCWEB_LOG_ERROR << "Value Illegal "
565b943aaefSJames Feist                                                      << *ptr;
566b943aaefSJames Feist                                     messages::internalError(asyncResp->res);
567b943aaefSJames Feist                                     return;
568b943aaefSJames Feist                                 }
569b943aaefSJames Feist                             }
570b943aaefSJames Feist                             // doubles
5715b4aa86bSJames Feist                             else if (propertyPair.first ==
5725b4aa86bSJames Feist                                          "FFGainCoefficient" ||
5735b4aa86bSJames Feist                                      propertyPair.first == "FFOffCoefficient" ||
5745b4aa86bSJames Feist                                      propertyPair.first == "ICoefficient" ||
5755b4aa86bSJames Feist                                      propertyPair.first == "ILimitMax" ||
5765b4aa86bSJames Feist                                      propertyPair.first == "ILimitMin" ||
577aad1a257SJames Feist                                      propertyPair.first ==
578aad1a257SJames Feist                                          "PositiveHysteresis" ||
579aad1a257SJames Feist                                      propertyPair.first ==
580aad1a257SJames Feist                                          "NegativeHysteresis" ||
5815b4aa86bSJames Feist                                      propertyPair.first == "OutLimitMax" ||
5825b4aa86bSJames Feist                                      propertyPair.first == "OutLimitMin" ||
5835b4aa86bSJames Feist                                      propertyPair.first == "PCoefficient" ||
5847625cb81SJames Feist                                      propertyPair.first == "SetPoint" ||
5855b4aa86bSJames Feist                                      propertyPair.first == "SlewNeg" ||
5865b4aa86bSJames Feist                                      propertyPair.first == "SlewPos")
5875b4aa86bSJames Feist                             {
5885b4aa86bSJames Feist                                 const double* ptr =
589abf2add6SEd Tanous                                     std::get_if<double>(&propertyPair.second);
5905b4aa86bSJames Feist                                 if (ptr == nullptr)
5915b4aa86bSJames Feist                                 {
5925b4aa86bSJames Feist                                     BMCWEB_LOG_ERROR << "Field Illegal "
5935b4aa86bSJames Feist                                                      << propertyPair.first;
594f12894f8SJason M. Bills                                     messages::internalError(asyncResp->res);
5955b4aa86bSJames Feist                                     return;
5965b4aa86bSJames Feist                                 }
597b7a08d04SJames Feist                                 (*config)[propertyPair.first] = *ptr;
5985b4aa86bSJames Feist                             }
5995b4aa86bSJames Feist                         }
6005b4aa86bSJames Feist                     }
6015b4aa86bSJames Feist                 }
6025b4aa86bSJames Feist             }
6035b4aa86bSJames Feist         },
6045b4aa86bSJames Feist         connection, path, objectManagerIface, "GetManagedObjects");
6055b4aa86bSJames Feist }
606ca537928SJennifer Lee 
60783ff9ab6SJames Feist enum class CreatePIDRet
60883ff9ab6SJames Feist {
60983ff9ab6SJames Feist     fail,
61083ff9ab6SJames Feist     del,
61183ff9ab6SJames Feist     patch
61283ff9ab6SJames Feist };
61383ff9ab6SJames Feist 
6145f2caaefSJames Feist static bool getZonesFromJsonReq(const std::shared_ptr<AsyncResp>& response,
6155f2caaefSJames Feist                                 std::vector<nlohmann::json>& config,
6165f2caaefSJames Feist                                 std::vector<std::string>& zones)
6175f2caaefSJames Feist {
618b6baeaa4SJames Feist     if (config.empty())
619b6baeaa4SJames Feist     {
620b6baeaa4SJames Feist         BMCWEB_LOG_ERROR << "Empty Zones";
621b6baeaa4SJames Feist         messages::propertyValueFormatError(response->res,
622b6baeaa4SJames Feist                                            nlohmann::json::array(), "Zones");
623b6baeaa4SJames Feist         return false;
624b6baeaa4SJames Feist     }
6255f2caaefSJames Feist     for (auto& odata : config)
6265f2caaefSJames Feist     {
6275f2caaefSJames Feist         std::string path;
6285f2caaefSJames Feist         if (!redfish::json_util::readJson(odata, response->res, "@odata.id",
6295f2caaefSJames Feist                                           path))
6305f2caaefSJames Feist         {
6315f2caaefSJames Feist             return false;
6325f2caaefSJames Feist         }
6335f2caaefSJames Feist         std::string input;
63461adbda3SJames Feist 
63561adbda3SJames Feist         // 8 below comes from
63661adbda3SJames Feist         // /redfish/v1/Managers/bmc#/Oem/OpenBmc/Fan/FanZones/Left
63761adbda3SJames Feist         //     0    1     2      3    4    5      6     7      8
63861adbda3SJames Feist         if (!dbus::utility::getNthStringFromPath(path, 8, input))
6395f2caaefSJames Feist         {
6405f2caaefSJames Feist             BMCWEB_LOG_ERROR << "Got invalid path " << path;
6415f2caaefSJames Feist             BMCWEB_LOG_ERROR << "Illegal Type Zones";
6425f2caaefSJames Feist             messages::propertyValueFormatError(response->res, odata.dump(),
6435f2caaefSJames Feist                                                "Zones");
6445f2caaefSJames Feist             return false;
6455f2caaefSJames Feist         }
6465f2caaefSJames Feist         boost::replace_all(input, "_", " ");
6475f2caaefSJames Feist         zones.emplace_back(std::move(input));
6485f2caaefSJames Feist     }
6495f2caaefSJames Feist     return true;
6505f2caaefSJames Feist }
6515f2caaefSJames Feist 
65273df0db0SJames Feist static const dbus::utility::ManagedItem*
65373df0db0SJames Feist     findChassis(const dbus::utility::ManagedObjectType& managedObj,
654b6baeaa4SJames Feist                 const std::string& value, std::string& chassis)
655b6baeaa4SJames Feist {
656b6baeaa4SJames Feist     BMCWEB_LOG_DEBUG << "Find Chassis: " << value << "\n";
657b6baeaa4SJames Feist 
658b6baeaa4SJames Feist     std::string escaped = boost::replace_all_copy(value, " ", "_");
659b6baeaa4SJames Feist     escaped = "/" + escaped;
660b6baeaa4SJames Feist     auto it = std::find_if(
661b6baeaa4SJames Feist         managedObj.begin(), managedObj.end(), [&escaped](const auto& obj) {
662b6baeaa4SJames Feist             if (boost::algorithm::ends_with(obj.first.str, escaped))
663b6baeaa4SJames Feist             {
664b6baeaa4SJames Feist                 BMCWEB_LOG_DEBUG << "Matched " << obj.first.str << "\n";
665b6baeaa4SJames Feist                 return true;
666b6baeaa4SJames Feist             }
667b6baeaa4SJames Feist             return false;
668b6baeaa4SJames Feist         });
669b6baeaa4SJames Feist 
670b6baeaa4SJames Feist     if (it == managedObj.end())
671b6baeaa4SJames Feist     {
67273df0db0SJames Feist         return nullptr;
673b6baeaa4SJames Feist     }
674b6baeaa4SJames Feist     // 5 comes from <chassis-name> being the 5th element
675b6baeaa4SJames Feist     // /xyz/openbmc_project/inventory/system/chassis/<chassis-name>
67673df0db0SJames Feist     if (dbus::utility::getNthStringFromPath(it->first.str, 5, chassis))
67773df0db0SJames Feist     {
67873df0db0SJames Feist         return &(*it);
67973df0db0SJames Feist     }
68073df0db0SJames Feist 
68173df0db0SJames Feist     return nullptr;
682b6baeaa4SJames Feist }
683b6baeaa4SJames Feist 
68483ff9ab6SJames Feist static CreatePIDRet createPidInterface(
68583ff9ab6SJames Feist     const std::shared_ptr<AsyncResp>& response, const std::string& type,
686b6baeaa4SJames Feist     nlohmann::json::iterator it, const std::string& path,
68783ff9ab6SJames Feist     const dbus::utility::ManagedObjectType& managedObj, bool createNewObject,
68883ff9ab6SJames Feist     boost::container::flat_map<std::string, dbus::utility::DbusVariantType>&
68983ff9ab6SJames Feist         output,
69073df0db0SJames Feist     std::string& chassis, const std::string& profile)
69183ff9ab6SJames Feist {
69283ff9ab6SJames Feist 
6935f2caaefSJames Feist     // common deleter
694b6baeaa4SJames Feist     if (it.value() == nullptr)
6955f2caaefSJames Feist     {
6965f2caaefSJames Feist         std::string iface;
6975f2caaefSJames Feist         if (type == "PidControllers" || type == "FanControllers")
6985f2caaefSJames Feist         {
6995f2caaefSJames Feist             iface = pidConfigurationIface;
7005f2caaefSJames Feist         }
7015f2caaefSJames Feist         else if (type == "FanZones")
7025f2caaefSJames Feist         {
7035f2caaefSJames Feist             iface = pidZoneConfigurationIface;
7045f2caaefSJames Feist         }
7055f2caaefSJames Feist         else if (type == "StepwiseControllers")
7065f2caaefSJames Feist         {
7075f2caaefSJames Feist             iface = stepwiseConfigurationIface;
7085f2caaefSJames Feist         }
7095f2caaefSJames Feist         else
7105f2caaefSJames Feist         {
7115f2caaefSJames Feist             BMCWEB_LOG_ERROR << "Line:" << __LINE__ << ", Illegal Type "
7125f2caaefSJames Feist                              << type;
7135f2caaefSJames Feist             messages::propertyUnknown(response->res, type);
7145f2caaefSJames Feist             return CreatePIDRet::fail;
7155f2caaefSJames Feist         }
7166ee7f774SJames Feist 
7176ee7f774SJames Feist         BMCWEB_LOG_DEBUG << "del " << path << " " << iface << "\n";
7185f2caaefSJames Feist         // delete interface
7195f2caaefSJames Feist         crow::connections::systemBus->async_method_call(
7205f2caaefSJames Feist             [response, path](const boost::system::error_code ec) {
7215f2caaefSJames Feist                 if (ec)
7225f2caaefSJames Feist                 {
7235f2caaefSJames Feist                     BMCWEB_LOG_ERROR << "Error patching " << path << ": " << ec;
7245f2caaefSJames Feist                     messages::internalError(response->res);
725b6baeaa4SJames Feist                     return;
7265f2caaefSJames Feist                 }
727b6baeaa4SJames Feist                 messages::success(response->res);
7285f2caaefSJames Feist             },
7295f2caaefSJames Feist             "xyz.openbmc_project.EntityManager", path, iface, "Delete");
7305f2caaefSJames Feist         return CreatePIDRet::del;
7315f2caaefSJames Feist     }
7325f2caaefSJames Feist 
73373df0db0SJames Feist     const dbus::utility::ManagedItem* managedItem = nullptr;
734b6baeaa4SJames Feist     if (!createNewObject)
735b6baeaa4SJames Feist     {
736b6baeaa4SJames Feist         // if we aren't creating a new object, we should be able to find it on
737b6baeaa4SJames Feist         // d-bus
73873df0db0SJames Feist         managedItem = findChassis(managedObj, it.key(), chassis);
73973df0db0SJames Feist         if (managedItem == nullptr)
740b6baeaa4SJames Feist         {
741b6baeaa4SJames Feist             BMCWEB_LOG_ERROR << "Failed to get chassis from config patch";
742b6baeaa4SJames Feist             messages::invalidObject(response->res, it.key());
743b6baeaa4SJames Feist             return CreatePIDRet::fail;
744b6baeaa4SJames Feist         }
745b6baeaa4SJames Feist     }
746b6baeaa4SJames Feist 
74773df0db0SJames Feist     if (profile.size() &&
74873df0db0SJames Feist         (type == "PidControllers" || type == "FanControllers" ||
74973df0db0SJames Feist          type == "StepwiseControllers"))
75073df0db0SJames Feist     {
75173df0db0SJames Feist         if (managedItem == nullptr)
75273df0db0SJames Feist         {
75373df0db0SJames Feist             output["Profiles"] = std::vector<std::string>{profile};
75473df0db0SJames Feist         }
75573df0db0SJames Feist         else
75673df0db0SJames Feist         {
75773df0db0SJames Feist             std::string interface;
75873df0db0SJames Feist             if (type == "StepwiseControllers")
75973df0db0SJames Feist             {
76073df0db0SJames Feist                 interface = stepwiseConfigurationIface;
76173df0db0SJames Feist             }
76273df0db0SJames Feist             else
76373df0db0SJames Feist             {
76473df0db0SJames Feist                 interface = pidConfigurationIface;
76573df0db0SJames Feist             }
76673df0db0SJames Feist             auto findConfig = managedItem->second.find(interface);
76773df0db0SJames Feist             if (findConfig == managedItem->second.end())
76873df0db0SJames Feist             {
76973df0db0SJames Feist                 BMCWEB_LOG_ERROR
77073df0db0SJames Feist                     << "Failed to find interface in managed object";
77173df0db0SJames Feist                 messages::internalError(response->res);
77273df0db0SJames Feist                 return CreatePIDRet::fail;
77373df0db0SJames Feist             }
77473df0db0SJames Feist             auto findProfiles = findConfig->second.find("Profiles");
77573df0db0SJames Feist             if (findProfiles != findConfig->second.end())
77673df0db0SJames Feist             {
77773df0db0SJames Feist                 const std::vector<std::string>* curProfiles =
77873df0db0SJames Feist                     std::get_if<std::vector<std::string>>(
77973df0db0SJames Feist                         &(findProfiles->second));
78073df0db0SJames Feist                 if (curProfiles == nullptr)
78173df0db0SJames Feist                 {
78273df0db0SJames Feist                     BMCWEB_LOG_ERROR << "Illegal profiles in managed object";
78373df0db0SJames Feist                     messages::internalError(response->res);
78473df0db0SJames Feist                     return CreatePIDRet::fail;
78573df0db0SJames Feist                 }
78673df0db0SJames Feist                 if (std::find(curProfiles->begin(), curProfiles->end(),
78773df0db0SJames Feist                               profile) == curProfiles->end())
78873df0db0SJames Feist                 {
78973df0db0SJames Feist                     std::vector<std::string> newProfiles = *curProfiles;
79073df0db0SJames Feist                     newProfiles.push_back(profile);
79173df0db0SJames Feist                     output["Profiles"] = newProfiles;
79273df0db0SJames Feist                 }
79373df0db0SJames Feist             }
79473df0db0SJames Feist         }
79573df0db0SJames Feist     }
79673df0db0SJames Feist 
79783ff9ab6SJames Feist     if (type == "PidControllers" || type == "FanControllers")
79883ff9ab6SJames Feist     {
79983ff9ab6SJames Feist         if (createNewObject)
80083ff9ab6SJames Feist         {
80183ff9ab6SJames Feist             output["Class"] = type == "PidControllers" ? std::string("temp")
80283ff9ab6SJames Feist                                                        : std::string("fan");
80383ff9ab6SJames Feist             output["Type"] = std::string("Pid");
80483ff9ab6SJames Feist         }
8055f2caaefSJames Feist 
8065f2caaefSJames Feist         std::optional<std::vector<nlohmann::json>> zones;
8075f2caaefSJames Feist         std::optional<std::vector<std::string>> inputs;
8085f2caaefSJames Feist         std::optional<std::vector<std::string>> outputs;
8095f2caaefSJames Feist         std::map<std::string, std::optional<double>> doubles;
810b943aaefSJames Feist         std::optional<std::string> setpointOffset;
8115f2caaefSJames Feist         if (!redfish::json_util::readJson(
812b6baeaa4SJames Feist                 it.value(), response->res, "Inputs", inputs, "Outputs", outputs,
8135f2caaefSJames Feist                 "Zones", zones, "FFGainCoefficient",
8145f2caaefSJames Feist                 doubles["FFGainCoefficient"], "FFOffCoefficient",
8155f2caaefSJames Feist                 doubles["FFOffCoefficient"], "ICoefficient",
8165f2caaefSJames Feist                 doubles["ICoefficient"], "ILimitMax", doubles["ILimitMax"],
8175f2caaefSJames Feist                 "ILimitMin", doubles["ILimitMin"], "OutLimitMax",
8185f2caaefSJames Feist                 doubles["OutLimitMax"], "OutLimitMin", doubles["OutLimitMin"],
8195f2caaefSJames Feist                 "PCoefficient", doubles["PCoefficient"], "SetPoint",
820b943aaefSJames Feist                 doubles["SetPoint"], "SetPointOffset", setpointOffset,
821b943aaefSJames Feist                 "SlewNeg", doubles["SlewNeg"], "SlewPos", doubles["SlewPos"],
822b943aaefSJames Feist                 "PositiveHysteresis", doubles["PositiveHysteresis"],
823b943aaefSJames Feist                 "NegativeHysteresis", doubles["NegativeHysteresis"]))
82483ff9ab6SJames Feist         {
8255f2caaefSJames Feist             BMCWEB_LOG_ERROR << "Line:" << __LINE__ << ", Illegal Property "
826b6baeaa4SJames Feist                              << it.value().dump();
8275f2caaefSJames Feist             return CreatePIDRet::fail;
82883ff9ab6SJames Feist         }
8295f2caaefSJames Feist         if (zones)
8305f2caaefSJames Feist         {
8315f2caaefSJames Feist             std::vector<std::string> zonesStr;
8325f2caaefSJames Feist             if (!getZonesFromJsonReq(response, *zones, zonesStr))
8335f2caaefSJames Feist             {
8345f2caaefSJames Feist                 BMCWEB_LOG_ERROR << "Line:" << __LINE__ << ", Illegal Zones";
8355f2caaefSJames Feist                 return CreatePIDRet::fail;
8365f2caaefSJames Feist             }
837b6baeaa4SJames Feist             if (chassis.empty() &&
838b6baeaa4SJames Feist                 !findChassis(managedObj, zonesStr[0], chassis))
839b6baeaa4SJames Feist             {
840b6baeaa4SJames Feist                 BMCWEB_LOG_ERROR << "Failed to get chassis from config patch";
841b6baeaa4SJames Feist                 messages::invalidObject(response->res, it.key());
842b6baeaa4SJames Feist                 return CreatePIDRet::fail;
843b6baeaa4SJames Feist             }
844b6baeaa4SJames Feist 
8455f2caaefSJames Feist             output["Zones"] = std::move(zonesStr);
8465f2caaefSJames Feist         }
8475f2caaefSJames Feist         if (inputs || outputs)
8485f2caaefSJames Feist         {
8495f2caaefSJames Feist             std::array<std::optional<std::vector<std::string>>*, 2> containers =
8505f2caaefSJames Feist                 {&inputs, &outputs};
8515f2caaefSJames Feist             size_t index = 0;
8525f2caaefSJames Feist             for (const auto& containerPtr : containers)
8535f2caaefSJames Feist             {
8545f2caaefSJames Feist                 std::optional<std::vector<std::string>>& container =
8555f2caaefSJames Feist                     *containerPtr;
8565f2caaefSJames Feist                 if (!container)
8575f2caaefSJames Feist                 {
8585f2caaefSJames Feist                     index++;
8595f2caaefSJames Feist                     continue;
86083ff9ab6SJames Feist                 }
86183ff9ab6SJames Feist 
8625f2caaefSJames Feist                 for (std::string& value : *container)
86383ff9ab6SJames Feist                 {
8645f2caaefSJames Feist                     boost::replace_all(value, "_", " ");
86583ff9ab6SJames Feist                 }
8665f2caaefSJames Feist                 std::string key;
8675f2caaefSJames Feist                 if (index == 0)
8685f2caaefSJames Feist                 {
8695f2caaefSJames Feist                     key = "Inputs";
8705f2caaefSJames Feist                 }
8715f2caaefSJames Feist                 else
8725f2caaefSJames Feist                 {
8735f2caaefSJames Feist                     key = "Outputs";
8745f2caaefSJames Feist                 }
8755f2caaefSJames Feist                 output[key] = *container;
8765f2caaefSJames Feist                 index++;
8775f2caaefSJames Feist             }
87883ff9ab6SJames Feist         }
87983ff9ab6SJames Feist 
880b943aaefSJames Feist         if (setpointOffset)
881b943aaefSJames Feist         {
882b943aaefSJames Feist             // translate between redfish and dbus names
883b943aaefSJames Feist             if (*setpointOffset == "UpperThresholdNonCritical")
884b943aaefSJames Feist             {
885b943aaefSJames Feist                 output["SetPointOffset"] = std::string("WarningLow");
886b943aaefSJames Feist             }
887b943aaefSJames Feist             else if (*setpointOffset == "LowerThresholdNonCritical")
888b943aaefSJames Feist             {
889b943aaefSJames Feist                 output["SetPointOffset"] = std::string("WarningHigh");
890b943aaefSJames Feist             }
891b943aaefSJames Feist             else if (*setpointOffset == "LowerThresholdCritical")
892b943aaefSJames Feist             {
893b943aaefSJames Feist                 output["SetPointOffset"] = std::string("CriticalLow");
894b943aaefSJames Feist             }
895b943aaefSJames Feist             else if (*setpointOffset == "UpperThresholdCritical")
896b943aaefSJames Feist             {
897b943aaefSJames Feist                 output["SetPointOffset"] = std::string("CriticalHigh");
898b943aaefSJames Feist             }
899b943aaefSJames Feist             else
900b943aaefSJames Feist             {
901b943aaefSJames Feist                 BMCWEB_LOG_ERROR << "Invalid setpointoffset "
902b943aaefSJames Feist                                  << *setpointOffset;
903b943aaefSJames Feist                 messages::invalidObject(response->res, it.key());
904b943aaefSJames Feist                 return CreatePIDRet::fail;
905b943aaefSJames Feist             }
906b943aaefSJames Feist         }
907b943aaefSJames Feist 
90883ff9ab6SJames Feist         // doubles
9095f2caaefSJames Feist         for (const auto& pairs : doubles)
91083ff9ab6SJames Feist         {
9115f2caaefSJames Feist             if (!pairs.second)
91283ff9ab6SJames Feist             {
9135f2caaefSJames Feist                 continue;
91483ff9ab6SJames Feist             }
9155f2caaefSJames Feist             BMCWEB_LOG_DEBUG << pairs.first << " = " << *pairs.second;
9165f2caaefSJames Feist             output[pairs.first] = *(pairs.second);
9175f2caaefSJames Feist         }
91883ff9ab6SJames Feist     }
91983ff9ab6SJames Feist 
92083ff9ab6SJames Feist     else if (type == "FanZones")
92183ff9ab6SJames Feist     {
92283ff9ab6SJames Feist         output["Type"] = std::string("Pid.Zone");
92383ff9ab6SJames Feist 
9245f2caaefSJames Feist         std::optional<nlohmann::json> chassisContainer;
9255f2caaefSJames Feist         std::optional<double> failSafePercent;
926d3ec07f8SJames Feist         std::optional<double> minThermalOutput;
927b6baeaa4SJames Feist         if (!redfish::json_util::readJson(it.value(), response->res, "Chassis",
9285f2caaefSJames Feist                                           chassisContainer, "FailSafePercent",
929d3ec07f8SJames Feist                                           failSafePercent, "MinThermalOutput",
930d3ec07f8SJames Feist                                           minThermalOutput))
93183ff9ab6SJames Feist         {
9325f2caaefSJames Feist             BMCWEB_LOG_ERROR << "Line:" << __LINE__ << ", Illegal Property "
933b6baeaa4SJames Feist                              << it.value().dump();
93483ff9ab6SJames Feist             return CreatePIDRet::fail;
93583ff9ab6SJames Feist         }
9365f2caaefSJames Feist 
9375f2caaefSJames Feist         if (chassisContainer)
93883ff9ab6SJames Feist         {
9395f2caaefSJames Feist 
9405f2caaefSJames Feist             std::string chassisId;
9415f2caaefSJames Feist             if (!redfish::json_util::readJson(*chassisContainer, response->res,
9425f2caaefSJames Feist                                               "@odata.id", chassisId))
9435f2caaefSJames Feist             {
9445f2caaefSJames Feist                 BMCWEB_LOG_ERROR << "Line:" << __LINE__ << ", Illegal Property "
9455f2caaefSJames Feist                                  << chassisContainer->dump();
94683ff9ab6SJames Feist                 return CreatePIDRet::fail;
94783ff9ab6SJames Feist             }
94883ff9ab6SJames Feist 
949717794d5SAppaRao Puli             // /redfish/v1/chassis/chassis_name/
9505f2caaefSJames Feist             if (!dbus::utility::getNthStringFromPath(chassisId, 3, chassis))
95183ff9ab6SJames Feist             {
9525f2caaefSJames Feist                 BMCWEB_LOG_ERROR << "Got invalid path " << chassisId;
9535f2caaefSJames Feist                 messages::invalidObject(response->res, chassisId);
95483ff9ab6SJames Feist                 return CreatePIDRet::fail;
95583ff9ab6SJames Feist             }
95683ff9ab6SJames Feist         }
957d3ec07f8SJames Feist         if (minThermalOutput)
95883ff9ab6SJames Feist         {
959d3ec07f8SJames Feist             output["MinThermalOutput"] = *minThermalOutput;
9605f2caaefSJames Feist         }
9615f2caaefSJames Feist         if (failSafePercent)
96283ff9ab6SJames Feist         {
9635f2caaefSJames Feist             output["FailSafePercent"] = *failSafePercent;
9645f2caaefSJames Feist         }
9655f2caaefSJames Feist     }
9665f2caaefSJames Feist     else if (type == "StepwiseControllers")
9675f2caaefSJames Feist     {
9685f2caaefSJames Feist         output["Type"] = std::string("Stepwise");
9695f2caaefSJames Feist 
9705f2caaefSJames Feist         std::optional<std::vector<nlohmann::json>> zones;
9715f2caaefSJames Feist         std::optional<std::vector<nlohmann::json>> steps;
9725f2caaefSJames Feist         std::optional<std::vector<std::string>> inputs;
9735f2caaefSJames Feist         std::optional<double> positiveHysteresis;
9745f2caaefSJames Feist         std::optional<double> negativeHysteresis;
975c33a90ecSJames Feist         std::optional<std::string> direction; // upper clipping curve vs lower
9765f2caaefSJames Feist         if (!redfish::json_util::readJson(
977b6baeaa4SJames Feist                 it.value(), response->res, "Zones", zones, "Steps", steps,
978b6baeaa4SJames Feist                 "Inputs", inputs, "PositiveHysteresis", positiveHysteresis,
979c33a90ecSJames Feist                 "NegativeHysteresis", negativeHysteresis, "Direction",
980c33a90ecSJames Feist                 direction))
9815f2caaefSJames Feist         {
9825f2caaefSJames Feist             BMCWEB_LOG_ERROR << "Line:" << __LINE__ << ", Illegal Property "
983b6baeaa4SJames Feist                              << it.value().dump();
98483ff9ab6SJames Feist             return CreatePIDRet::fail;
98583ff9ab6SJames Feist         }
9865f2caaefSJames Feist 
9875f2caaefSJames Feist         if (zones)
98883ff9ab6SJames Feist         {
989b6baeaa4SJames Feist             std::vector<std::string> zonesStrs;
990b6baeaa4SJames Feist             if (!getZonesFromJsonReq(response, *zones, zonesStrs))
9915f2caaefSJames Feist             {
9925f2caaefSJames Feist                 BMCWEB_LOG_ERROR << "Line:" << __LINE__ << ", Illegal Zones";
99383ff9ab6SJames Feist                 return CreatePIDRet::fail;
99483ff9ab6SJames Feist             }
995b6baeaa4SJames Feist             if (chassis.empty() &&
996b6baeaa4SJames Feist                 !findChassis(managedObj, zonesStrs[0], chassis))
997b6baeaa4SJames Feist             {
998b6baeaa4SJames Feist                 BMCWEB_LOG_ERROR << "Failed to get chassis from config patch";
999b6baeaa4SJames Feist                 messages::invalidObject(response->res, it.key());
1000b6baeaa4SJames Feist                 return CreatePIDRet::fail;
1001b6baeaa4SJames Feist             }
1002b6baeaa4SJames Feist             output["Zones"] = std::move(zonesStrs);
10035f2caaefSJames Feist         }
10045f2caaefSJames Feist         if (steps)
10055f2caaefSJames Feist         {
10065f2caaefSJames Feist             std::vector<double> readings;
10075f2caaefSJames Feist             std::vector<double> outputs;
10085f2caaefSJames Feist             for (auto& step : *steps)
10095f2caaefSJames Feist             {
10105f2caaefSJames Feist                 double target;
1011b01bf299SEd Tanous                 double output;
10125f2caaefSJames Feist 
10135f2caaefSJames Feist                 if (!redfish::json_util::readJson(step, response->res, "Target",
1014b01bf299SEd Tanous                                                   target, "Output", output))
10155f2caaefSJames Feist                 {
10165f2caaefSJames Feist                     BMCWEB_LOG_ERROR << "Line:" << __LINE__
1017b6baeaa4SJames Feist                                      << ", Illegal Property "
1018b6baeaa4SJames Feist                                      << it.value().dump();
10195f2caaefSJames Feist                     return CreatePIDRet::fail;
10205f2caaefSJames Feist                 }
10215f2caaefSJames Feist                 readings.emplace_back(target);
1022b01bf299SEd Tanous                 outputs.emplace_back(output);
10235f2caaefSJames Feist             }
10245f2caaefSJames Feist             output["Reading"] = std::move(readings);
10255f2caaefSJames Feist             output["Output"] = std::move(outputs);
10265f2caaefSJames Feist         }
10275f2caaefSJames Feist         if (inputs)
10285f2caaefSJames Feist         {
10295f2caaefSJames Feist             for (std::string& value : *inputs)
10305f2caaefSJames Feist             {
10315f2caaefSJames Feist                 boost::replace_all(value, "_", " ");
10325f2caaefSJames Feist             }
10335f2caaefSJames Feist             output["Inputs"] = std::move(*inputs);
10345f2caaefSJames Feist         }
10355f2caaefSJames Feist         if (negativeHysteresis)
10365f2caaefSJames Feist         {
10375f2caaefSJames Feist             output["NegativeHysteresis"] = *negativeHysteresis;
10385f2caaefSJames Feist         }
10395f2caaefSJames Feist         if (positiveHysteresis)
10405f2caaefSJames Feist         {
10415f2caaefSJames Feist             output["PositiveHysteresis"] = *positiveHysteresis;
104283ff9ab6SJames Feist         }
1043c33a90ecSJames Feist         if (direction)
1044c33a90ecSJames Feist         {
1045c33a90ecSJames Feist             constexpr const std::array<const char*, 2> allowedDirections = {
1046c33a90ecSJames Feist                 "Ceiling", "Floor"};
1047c33a90ecSJames Feist             if (std::find(allowedDirections.begin(), allowedDirections.end(),
1048c33a90ecSJames Feist                           *direction) == allowedDirections.end())
1049c33a90ecSJames Feist             {
1050c33a90ecSJames Feist                 messages::propertyValueTypeError(response->res, "Direction",
1051c33a90ecSJames Feist                                                  *direction);
1052c33a90ecSJames Feist                 return CreatePIDRet::fail;
1053c33a90ecSJames Feist             }
1054c33a90ecSJames Feist             output["Class"] = *direction;
1055c33a90ecSJames Feist         }
105683ff9ab6SJames Feist     }
105783ff9ab6SJames Feist     else
105883ff9ab6SJames Feist     {
10595f2caaefSJames Feist         BMCWEB_LOG_ERROR << "Line:" << __LINE__ << ", Illegal Type " << type;
106035a62c7cSJason M. Bills         messages::propertyUnknown(response->res, type);
106183ff9ab6SJames Feist         return CreatePIDRet::fail;
106283ff9ab6SJames Feist     }
106383ff9ab6SJames Feist     return CreatePIDRet::patch;
106483ff9ab6SJames Feist }
106573df0db0SJames Feist struct GetPIDValues : std::enable_shared_from_this<GetPIDValues>
106673df0db0SJames Feist {
106783ff9ab6SJames Feist 
106873df0db0SJames Feist     GetPIDValues(const std::shared_ptr<AsyncResp>& asyncResp) :
106973df0db0SJames Feist         asyncResp(asyncResp)
107073df0db0SJames Feist 
1071*1214b7e7SGunnar Mills     {}
10729c310685SBorawski.Lukasz 
107373df0db0SJames Feist     void run()
10745b4aa86bSJames Feist     {
107573df0db0SJames Feist         std::shared_ptr<GetPIDValues> self = shared_from_this();
107673df0db0SJames Feist 
107773df0db0SJames Feist         // get all configurations
10785b4aa86bSJames Feist         crow::connections::systemBus->async_method_call(
107973df0db0SJames Feist             [self](const boost::system::error_code ec,
10805b4aa86bSJames Feist                    const crow::openbmc_mapper::GetSubTreeType& subtree) {
10815b4aa86bSJames Feist                 if (ec)
10825b4aa86bSJames Feist                 {
10835b4aa86bSJames Feist                     BMCWEB_LOG_ERROR << ec;
108473df0db0SJames Feist                     messages::internalError(self->asyncResp->res);
108573df0db0SJames Feist                     return;
108673df0db0SJames Feist                 }
108773df0db0SJames Feist                 self->subtree = subtree;
108873df0db0SJames Feist             },
108973df0db0SJames Feist             "xyz.openbmc_project.ObjectMapper",
109073df0db0SJames Feist             "/xyz/openbmc_project/object_mapper",
109173df0db0SJames Feist             "xyz.openbmc_project.ObjectMapper", "GetSubTree", "/", 0,
109273df0db0SJames Feist             std::array<const char*, 4>{
109373df0db0SJames Feist                 pidConfigurationIface, pidZoneConfigurationIface,
109473df0db0SJames Feist                 objectManagerIface, stepwiseConfigurationIface});
109573df0db0SJames Feist 
109673df0db0SJames Feist         // at the same time get the selected profile
109773df0db0SJames Feist         crow::connections::systemBus->async_method_call(
109873df0db0SJames Feist             [self](const boost::system::error_code ec,
109973df0db0SJames Feist                    const crow::openbmc_mapper::GetSubTreeType& subtree) {
110073df0db0SJames Feist                 if (ec || subtree.empty())
110173df0db0SJames Feist                 {
110273df0db0SJames Feist                     return;
110373df0db0SJames Feist                 }
110473df0db0SJames Feist                 if (subtree[0].second.size() != 1)
110573df0db0SJames Feist                 {
110673df0db0SJames Feist                     // invalid mapper response, should never happen
110773df0db0SJames Feist                     BMCWEB_LOG_ERROR << "GetPIDValues: Mapper Error";
110873df0db0SJames Feist                     messages::internalError(self->asyncResp->res);
11095b4aa86bSJames Feist                     return;
11105b4aa86bSJames Feist                 }
11115b4aa86bSJames Feist 
111273df0db0SJames Feist                 const std::string& path = subtree[0].first;
111373df0db0SJames Feist                 const std::string& owner = subtree[0].second[0].first;
111473df0db0SJames Feist                 crow::connections::systemBus->async_method_call(
111573df0db0SJames Feist                     [path, owner, self](
111673df0db0SJames Feist                         const boost::system::error_code ec,
111773df0db0SJames Feist                         const boost::container::flat_map<
111873df0db0SJames Feist                             std::string, std::variant<std::vector<std::string>,
111973df0db0SJames Feist                                                       std::string>>& resp) {
112073df0db0SJames Feist                         if (ec)
112173df0db0SJames Feist                         {
112273df0db0SJames Feist                             BMCWEB_LOG_ERROR << "GetPIDValues: Can't get "
112373df0db0SJames Feist                                                 "thermalModeIface "
112473df0db0SJames Feist                                              << path;
112573df0db0SJames Feist                             messages::internalError(self->asyncResp->res);
112673df0db0SJames Feist                             return;
112773df0db0SJames Feist                         }
1128271584abSEd Tanous                         const std::string* current = nullptr;
1129271584abSEd Tanous                         const std::vector<std::string>* supported = nullptr;
113073df0db0SJames Feist                         for (auto& [key, value] : resp)
113173df0db0SJames Feist                         {
113273df0db0SJames Feist                             if (key == "Current")
113373df0db0SJames Feist                             {
113473df0db0SJames Feist                                 current = std::get_if<std::string>(&value);
113573df0db0SJames Feist                                 if (current == nullptr)
113673df0db0SJames Feist                                 {
113773df0db0SJames Feist                                     BMCWEB_LOG_ERROR
113873df0db0SJames Feist                                         << "GetPIDValues: thermal mode "
113973df0db0SJames Feist                                            "iface invalid "
114073df0db0SJames Feist                                         << path;
114173df0db0SJames Feist                                     messages::internalError(
114273df0db0SJames Feist                                         self->asyncResp->res);
114373df0db0SJames Feist                                     return;
114473df0db0SJames Feist                                 }
114573df0db0SJames Feist                             }
114673df0db0SJames Feist                             if (key == "Supported")
114773df0db0SJames Feist                             {
114873df0db0SJames Feist                                 supported =
114973df0db0SJames Feist                                     std::get_if<std::vector<std::string>>(
115073df0db0SJames Feist                                         &value);
115173df0db0SJames Feist                                 if (supported == nullptr)
115273df0db0SJames Feist                                 {
115373df0db0SJames Feist                                     BMCWEB_LOG_ERROR
115473df0db0SJames Feist                                         << "GetPIDValues: thermal mode "
115573df0db0SJames Feist                                            "iface invalid"
115673df0db0SJames Feist                                         << path;
115773df0db0SJames Feist                                     messages::internalError(
115873df0db0SJames Feist                                         self->asyncResp->res);
115973df0db0SJames Feist                                     return;
116073df0db0SJames Feist                                 }
116173df0db0SJames Feist                             }
116273df0db0SJames Feist                         }
116373df0db0SJames Feist                         if (current == nullptr || supported == nullptr)
116473df0db0SJames Feist                         {
116573df0db0SJames Feist                             BMCWEB_LOG_ERROR << "GetPIDValues: thermal mode "
116673df0db0SJames Feist                                                 "iface invalid "
116773df0db0SJames Feist                                              << path;
116873df0db0SJames Feist                             messages::internalError(self->asyncResp->res);
116973df0db0SJames Feist                             return;
117073df0db0SJames Feist                         }
117173df0db0SJames Feist                         self->currentProfile = *current;
117273df0db0SJames Feist                         self->supportedProfiles = *supported;
117373df0db0SJames Feist                     },
117473df0db0SJames Feist                     owner, path, "org.freedesktop.DBus.Properties", "GetAll",
117573df0db0SJames Feist                     thermalModeIface);
117673df0db0SJames Feist             },
117773df0db0SJames Feist             "xyz.openbmc_project.ObjectMapper",
117873df0db0SJames Feist             "/xyz/openbmc_project/object_mapper",
117973df0db0SJames Feist             "xyz.openbmc_project.ObjectMapper", "GetSubTree", "/", 0,
118073df0db0SJames Feist             std::array<const char*, 1>{thermalModeIface});
118173df0db0SJames Feist     }
118273df0db0SJames Feist 
118373df0db0SJames Feist     ~GetPIDValues()
118473df0db0SJames Feist     {
118573df0db0SJames Feist         if (asyncResp->res.result() != boost::beast::http::status::ok)
118673df0db0SJames Feist         {
118773df0db0SJames Feist             return;
118873df0db0SJames Feist         }
11895b4aa86bSJames Feist         // create map of <connection, path to objMgr>>
119073df0db0SJames Feist         boost::container::flat_map<std::string, std::string> objectMgrPaths;
11916bce33bcSJames Feist         boost::container::flat_set<std::string> calledConnections;
11925b4aa86bSJames Feist         for (const auto& pathGroup : subtree)
11935b4aa86bSJames Feist         {
11945b4aa86bSJames Feist             for (const auto& connectionGroup : pathGroup.second)
11955b4aa86bSJames Feist             {
11966bce33bcSJames Feist                 auto findConnection =
11976bce33bcSJames Feist                     calledConnections.find(connectionGroup.first);
11986bce33bcSJames Feist                 if (findConnection != calledConnections.end())
11996bce33bcSJames Feist                 {
12006bce33bcSJames Feist                     break;
12016bce33bcSJames Feist                 }
120273df0db0SJames Feist                 for (const std::string& interface : connectionGroup.second)
12035b4aa86bSJames Feist                 {
12045b4aa86bSJames Feist                     if (interface == objectManagerIface)
12055b4aa86bSJames Feist                     {
120673df0db0SJames Feist                         objectMgrPaths[connectionGroup.first] = pathGroup.first;
12075b4aa86bSJames Feist                     }
12085b4aa86bSJames Feist                     // this list is alphabetical, so we
12095b4aa86bSJames Feist                     // should have found the objMgr by now
12105b4aa86bSJames Feist                     if (interface == pidConfigurationIface ||
1211b7a08d04SJames Feist                         interface == pidZoneConfigurationIface ||
1212b7a08d04SJames Feist                         interface == stepwiseConfigurationIface)
12135b4aa86bSJames Feist                     {
12145b4aa86bSJames Feist                         auto findObjMgr =
12155b4aa86bSJames Feist                             objectMgrPaths.find(connectionGroup.first);
12165b4aa86bSJames Feist                         if (findObjMgr == objectMgrPaths.end())
12175b4aa86bSJames Feist                         {
12185b4aa86bSJames Feist                             BMCWEB_LOG_DEBUG << connectionGroup.first
12195b4aa86bSJames Feist                                              << "Has no Object Manager";
12205b4aa86bSJames Feist                             continue;
12215b4aa86bSJames Feist                         }
12226bce33bcSJames Feist 
12236bce33bcSJames Feist                         calledConnections.insert(connectionGroup.first);
12246bce33bcSJames Feist 
122573df0db0SJames Feist                         asyncPopulatePid(findObjMgr->first, findObjMgr->second,
122673df0db0SJames Feist                                          currentProfile, supportedProfiles,
122773df0db0SJames Feist                                          asyncResp);
12285b4aa86bSJames Feist                         break;
12295b4aa86bSJames Feist                     }
12305b4aa86bSJames Feist                 }
12315b4aa86bSJames Feist             }
12325b4aa86bSJames Feist         }
123373df0db0SJames Feist     }
123473df0db0SJames Feist 
123573df0db0SJames Feist     std::vector<std::string> supportedProfiles;
123673df0db0SJames Feist     std::string currentProfile;
123773df0db0SJames Feist     crow::openbmc_mapper::GetSubTreeType subtree;
123873df0db0SJames Feist     std::shared_ptr<AsyncResp> asyncResp;
123973df0db0SJames Feist };
124073df0db0SJames Feist 
124173df0db0SJames Feist struct SetPIDValues : std::enable_shared_from_this<SetPIDValues>
124273df0db0SJames Feist {
124373df0db0SJames Feist 
1244271584abSEd Tanous     SetPIDValues(const std::shared_ptr<AsyncResp>& asyncRespIn,
124573df0db0SJames Feist                  nlohmann::json& data) :
1246271584abSEd Tanous         asyncResp(asyncRespIn)
124773df0db0SJames Feist     {
124873df0db0SJames Feist 
124973df0db0SJames Feist         std::optional<nlohmann::json> pidControllers;
125073df0db0SJames Feist         std::optional<nlohmann::json> fanControllers;
125173df0db0SJames Feist         std::optional<nlohmann::json> fanZones;
125273df0db0SJames Feist         std::optional<nlohmann::json> stepwiseControllers;
125373df0db0SJames Feist 
125473df0db0SJames Feist         if (!redfish::json_util::readJson(
125573df0db0SJames Feist                 data, asyncResp->res, "PidControllers", pidControllers,
125673df0db0SJames Feist                 "FanControllers", fanControllers, "FanZones", fanZones,
125773df0db0SJames Feist                 "StepwiseControllers", stepwiseControllers, "Profile", profile))
125873df0db0SJames Feist         {
125973df0db0SJames Feist             BMCWEB_LOG_ERROR << "Line:" << __LINE__ << ", Illegal Property "
126073df0db0SJames Feist                              << data.dump();
126173df0db0SJames Feist             return;
126273df0db0SJames Feist         }
126373df0db0SJames Feist         configuration.emplace_back("PidControllers", std::move(pidControllers));
126473df0db0SJames Feist         configuration.emplace_back("FanControllers", std::move(fanControllers));
126573df0db0SJames Feist         configuration.emplace_back("FanZones", std::move(fanZones));
126673df0db0SJames Feist         configuration.emplace_back("StepwiseControllers",
126773df0db0SJames Feist                                    std::move(stepwiseControllers));
126873df0db0SJames Feist     }
126973df0db0SJames Feist     void run()
127073df0db0SJames Feist     {
127173df0db0SJames Feist         if (asyncResp->res.result() != boost::beast::http::status::ok)
127273df0db0SJames Feist         {
127373df0db0SJames Feist             return;
127473df0db0SJames Feist         }
127573df0db0SJames Feist 
127673df0db0SJames Feist         std::shared_ptr<SetPIDValues> self = shared_from_this();
127773df0db0SJames Feist 
127873df0db0SJames Feist         // todo(james): might make sense to do a mapper call here if this
127973df0db0SJames Feist         // interface gets more traction
128073df0db0SJames Feist         crow::connections::systemBus->async_method_call(
128173df0db0SJames Feist             [self](const boost::system::error_code ec,
1282271584abSEd Tanous                    dbus::utility::ManagedObjectType& mObj) {
128373df0db0SJames Feist                 if (ec)
128473df0db0SJames Feist                 {
128573df0db0SJames Feist                     BMCWEB_LOG_ERROR << "Error communicating to Entity Manager";
128673df0db0SJames Feist                     messages::internalError(self->asyncResp->res);
128773df0db0SJames Feist                     return;
128873df0db0SJames Feist                 }
1289e69d9de2SJames Feist                 const std::array<const char*, 3> configurations = {
1290e69d9de2SJames Feist                     pidConfigurationIface, pidZoneConfigurationIface,
1291e69d9de2SJames Feist                     stepwiseConfigurationIface};
1292e69d9de2SJames Feist 
129314b0b8d5SJames Feist                 for (const auto& [path, object] : mObj)
1294e69d9de2SJames Feist                 {
129514b0b8d5SJames Feist                     for (const auto& [interface, _] : object)
1296e69d9de2SJames Feist                     {
1297e69d9de2SJames Feist                         if (std::find(configurations.begin(),
1298e69d9de2SJames Feist                                       configurations.end(),
1299e69d9de2SJames Feist                                       interface) != configurations.end())
1300e69d9de2SJames Feist                         {
130114b0b8d5SJames Feist                             self->objectCount++;
1302e69d9de2SJames Feist                             break;
1303e69d9de2SJames Feist                         }
1304e69d9de2SJames Feist                     }
1305e69d9de2SJames Feist                 }
1306271584abSEd Tanous                 self->managedObj = std::move(mObj);
130773df0db0SJames Feist             },
130873df0db0SJames Feist             "xyz.openbmc_project.EntityManager", "/", objectManagerIface,
130973df0db0SJames Feist             "GetManagedObjects");
131073df0db0SJames Feist 
131173df0db0SJames Feist         // at the same time get the profile information
131273df0db0SJames Feist         crow::connections::systemBus->async_method_call(
131373df0db0SJames Feist             [self](const boost::system::error_code ec,
131473df0db0SJames Feist                    const crow::openbmc_mapper::GetSubTreeType& subtree) {
131573df0db0SJames Feist                 if (ec || subtree.empty())
131673df0db0SJames Feist                 {
131773df0db0SJames Feist                     return;
131873df0db0SJames Feist                 }
131973df0db0SJames Feist                 if (subtree[0].second.empty())
132073df0db0SJames Feist                 {
132173df0db0SJames Feist                     // invalid mapper response, should never happen
132273df0db0SJames Feist                     BMCWEB_LOG_ERROR << "SetPIDValues: Mapper Error";
132373df0db0SJames Feist                     messages::internalError(self->asyncResp->res);
132473df0db0SJames Feist                     return;
132573df0db0SJames Feist                 }
132673df0db0SJames Feist 
132773df0db0SJames Feist                 const std::string& path = subtree[0].first;
132873df0db0SJames Feist                 const std::string& owner = subtree[0].second[0].first;
132973df0db0SJames Feist                 crow::connections::systemBus->async_method_call(
133073df0db0SJames Feist                     [self, path, owner](
133173df0db0SJames Feist                         const boost::system::error_code ec,
133273df0db0SJames Feist                         const boost::container::flat_map<
133373df0db0SJames Feist                             std::string, std::variant<std::vector<std::string>,
1334271584abSEd Tanous                                                       std::string>>& r) {
133573df0db0SJames Feist                         if (ec)
133673df0db0SJames Feist                         {
133773df0db0SJames Feist                             BMCWEB_LOG_ERROR << "SetPIDValues: Can't get "
133873df0db0SJames Feist                                                 "thermalModeIface "
133973df0db0SJames Feist                                              << path;
134073df0db0SJames Feist                             messages::internalError(self->asyncResp->res);
134173df0db0SJames Feist                             return;
134273df0db0SJames Feist                         }
1343271584abSEd Tanous                         const std::string* current = nullptr;
1344271584abSEd Tanous                         const std::vector<std::string>* supported = nullptr;
1345271584abSEd Tanous                         for (auto& [key, value] : r)
134673df0db0SJames Feist                         {
134773df0db0SJames Feist                             if (key == "Current")
134873df0db0SJames Feist                             {
134973df0db0SJames Feist                                 current = std::get_if<std::string>(&value);
135073df0db0SJames Feist                                 if (current == nullptr)
135173df0db0SJames Feist                                 {
135273df0db0SJames Feist                                     BMCWEB_LOG_ERROR
135373df0db0SJames Feist                                         << "SetPIDValues: thermal mode "
135473df0db0SJames Feist                                            "iface invalid "
135573df0db0SJames Feist                                         << path;
135673df0db0SJames Feist                                     messages::internalError(
135773df0db0SJames Feist                                         self->asyncResp->res);
135873df0db0SJames Feist                                     return;
135973df0db0SJames Feist                                 }
136073df0db0SJames Feist                             }
136173df0db0SJames Feist                             if (key == "Supported")
136273df0db0SJames Feist                             {
136373df0db0SJames Feist                                 supported =
136473df0db0SJames Feist                                     std::get_if<std::vector<std::string>>(
136573df0db0SJames Feist                                         &value);
136673df0db0SJames Feist                                 if (supported == nullptr)
136773df0db0SJames Feist                                 {
136873df0db0SJames Feist                                     BMCWEB_LOG_ERROR
136973df0db0SJames Feist                                         << "SetPIDValues: thermal mode "
137073df0db0SJames Feist                                            "iface invalid"
137173df0db0SJames Feist                                         << path;
137273df0db0SJames Feist                                     messages::internalError(
137373df0db0SJames Feist                                         self->asyncResp->res);
137473df0db0SJames Feist                                     return;
137573df0db0SJames Feist                                 }
137673df0db0SJames Feist                             }
137773df0db0SJames Feist                         }
137873df0db0SJames Feist                         if (current == nullptr || supported == nullptr)
137973df0db0SJames Feist                         {
138073df0db0SJames Feist                             BMCWEB_LOG_ERROR << "SetPIDValues: thermal mode "
138173df0db0SJames Feist                                                 "iface invalid "
138273df0db0SJames Feist                                              << path;
138373df0db0SJames Feist                             messages::internalError(self->asyncResp->res);
138473df0db0SJames Feist                             return;
138573df0db0SJames Feist                         }
138673df0db0SJames Feist                         self->currentProfile = *current;
138773df0db0SJames Feist                         self->supportedProfiles = *supported;
138873df0db0SJames Feist                         self->profileConnection = owner;
138973df0db0SJames Feist                         self->profilePath = path;
139073df0db0SJames Feist                     },
139173df0db0SJames Feist                     owner, path, "org.freedesktop.DBus.Properties", "GetAll",
139273df0db0SJames Feist                     thermalModeIface);
13935b4aa86bSJames Feist             },
13945b4aa86bSJames Feist             "xyz.openbmc_project.ObjectMapper",
13955b4aa86bSJames Feist             "/xyz/openbmc_project/object_mapper",
13965b4aa86bSJames Feist             "xyz.openbmc_project.ObjectMapper", "GetSubTree", "/", 0,
139773df0db0SJames Feist             std::array<const char*, 1>{thermalModeIface});
139873df0db0SJames Feist     }
139973df0db0SJames Feist     ~SetPIDValues()
140073df0db0SJames Feist     {
140173df0db0SJames Feist         if (asyncResp->res.result() != boost::beast::http::status::ok)
140273df0db0SJames Feist         {
140373df0db0SJames Feist             return;
14045b4aa86bSJames Feist         }
14055b4aa86bSJames Feist 
140673df0db0SJames Feist         std::shared_ptr<AsyncResp> response = asyncResp;
140773df0db0SJames Feist 
140873df0db0SJames Feist         if (profile)
140973df0db0SJames Feist         {
141073df0db0SJames Feist             if (std::find(supportedProfiles.begin(), supportedProfiles.end(),
141173df0db0SJames Feist                           *profile) == supportedProfiles.end())
141273df0db0SJames Feist             {
141373df0db0SJames Feist                 messages::actionParameterUnknown(response->res, "Profile",
141473df0db0SJames Feist                                                  *profile);
141573df0db0SJames Feist                 return;
141673df0db0SJames Feist             }
141773df0db0SJames Feist             currentProfile = *profile;
141873df0db0SJames Feist             crow::connections::systemBus->async_method_call(
141973df0db0SJames Feist                 [response](const boost::system::error_code ec) {
142073df0db0SJames Feist                     if (ec)
142173df0db0SJames Feist                     {
142273df0db0SJames Feist                         BMCWEB_LOG_ERROR << "Error patching profile" << ec;
142373df0db0SJames Feist                         messages::internalError(response->res);
142473df0db0SJames Feist                     }
142573df0db0SJames Feist                 },
142673df0db0SJames Feist                 profileConnection, profilePath,
142773df0db0SJames Feist                 "org.freedesktop.DBus.Properties", "Set", thermalModeIface,
142873df0db0SJames Feist                 "Current", std::variant<std::string>(*profile));
142973df0db0SJames Feist         }
143073df0db0SJames Feist 
143173df0db0SJames Feist         for (auto& containerPair : configuration)
143273df0db0SJames Feist         {
143373df0db0SJames Feist             auto& container = containerPair.second;
143473df0db0SJames Feist             if (!container)
143573df0db0SJames Feist             {
143673df0db0SJames Feist                 continue;
143773df0db0SJames Feist             }
14386ee7f774SJames Feist             BMCWEB_LOG_DEBUG << *container;
14396ee7f774SJames Feist 
144073df0db0SJames Feist             std::string& type = containerPair.first;
144173df0db0SJames Feist 
144273df0db0SJames Feist             for (nlohmann::json::iterator it = container->begin();
144373df0db0SJames Feist                  it != container->end(); it++)
144473df0db0SJames Feist             {
144573df0db0SJames Feist                 const auto& name = it.key();
14466ee7f774SJames Feist                 BMCWEB_LOG_DEBUG << "looking for " << name;
14476ee7f774SJames Feist 
144873df0db0SJames Feist                 auto pathItr =
144973df0db0SJames Feist                     std::find_if(managedObj.begin(), managedObj.end(),
145073df0db0SJames Feist                                  [&name](const auto& obj) {
145173df0db0SJames Feist                                      return boost::algorithm::ends_with(
145273df0db0SJames Feist                                          obj.first.str, "/" + name);
145373df0db0SJames Feist                                  });
145473df0db0SJames Feist                 boost::container::flat_map<std::string,
145573df0db0SJames Feist                                            dbus::utility::DbusVariantType>
145673df0db0SJames Feist                     output;
145773df0db0SJames Feist 
145873df0db0SJames Feist                 output.reserve(16); // The pid interface length
145973df0db0SJames Feist 
146073df0db0SJames Feist                 // determines if we're patching entity-manager or
146173df0db0SJames Feist                 // creating a new object
146273df0db0SJames Feist                 bool createNewObject = (pathItr == managedObj.end());
14636ee7f774SJames Feist                 BMCWEB_LOG_DEBUG << "Found = " << !createNewObject;
14646ee7f774SJames Feist 
146573df0db0SJames Feist                 std::string iface;
146673df0db0SJames Feist                 if (type == "PidControllers" || type == "FanControllers")
146773df0db0SJames Feist                 {
146873df0db0SJames Feist                     iface = pidConfigurationIface;
146973df0db0SJames Feist                     if (!createNewObject &&
147073df0db0SJames Feist                         pathItr->second.find(pidConfigurationIface) ==
147173df0db0SJames Feist                             pathItr->second.end())
147273df0db0SJames Feist                     {
147373df0db0SJames Feist                         createNewObject = true;
147473df0db0SJames Feist                     }
147573df0db0SJames Feist                 }
147673df0db0SJames Feist                 else if (type == "FanZones")
147773df0db0SJames Feist                 {
147873df0db0SJames Feist                     iface = pidZoneConfigurationIface;
147973df0db0SJames Feist                     if (!createNewObject &&
148073df0db0SJames Feist                         pathItr->second.find(pidZoneConfigurationIface) ==
148173df0db0SJames Feist                             pathItr->second.end())
148273df0db0SJames Feist                     {
148373df0db0SJames Feist 
148473df0db0SJames Feist                         createNewObject = true;
148573df0db0SJames Feist                     }
148673df0db0SJames Feist                 }
148773df0db0SJames Feist                 else if (type == "StepwiseControllers")
148873df0db0SJames Feist                 {
148973df0db0SJames Feist                     iface = stepwiseConfigurationIface;
149073df0db0SJames Feist                     if (!createNewObject &&
149173df0db0SJames Feist                         pathItr->second.find(stepwiseConfigurationIface) ==
149273df0db0SJames Feist                             pathItr->second.end())
149373df0db0SJames Feist                     {
149473df0db0SJames Feist                         createNewObject = true;
149573df0db0SJames Feist                     }
149673df0db0SJames Feist                 }
14976ee7f774SJames Feist 
14986ee7f774SJames Feist                 if (createNewObject && it.value() == nullptr)
14996ee7f774SJames Feist                 {
15006ee7f774SJames Feist                     // can't delete a non-existant object
15016ee7f774SJames Feist                     messages::invalidObject(response->res, name);
15026ee7f774SJames Feist                     continue;
15036ee7f774SJames Feist                 }
15046ee7f774SJames Feist 
15056ee7f774SJames Feist                 std::string path;
15066ee7f774SJames Feist                 if (pathItr != managedObj.end())
15076ee7f774SJames Feist                 {
15086ee7f774SJames Feist                     path = pathItr->first.str;
15096ee7f774SJames Feist                 }
15106ee7f774SJames Feist 
151173df0db0SJames Feist                 BMCWEB_LOG_DEBUG << "Create new = " << createNewObject << "\n";
1512e69d9de2SJames Feist 
1513e69d9de2SJames Feist                 // arbitrary limit to avoid attacks
1514e69d9de2SJames Feist                 constexpr const size_t controllerLimit = 500;
151514b0b8d5SJames Feist                 if (createNewObject && objectCount >= controllerLimit)
1516e69d9de2SJames Feist                 {
1517e69d9de2SJames Feist                     messages::resourceExhaustion(response->res, type);
1518e69d9de2SJames Feist                     continue;
1519e69d9de2SJames Feist                 }
1520e69d9de2SJames Feist 
152173df0db0SJames Feist                 output["Name"] = boost::replace_all_copy(name, "_", " ");
152273df0db0SJames Feist 
152373df0db0SJames Feist                 std::string chassis;
152473df0db0SJames Feist                 CreatePIDRet ret = createPidInterface(
15256ee7f774SJames Feist                     response, type, it, path, managedObj, createNewObject,
15266ee7f774SJames Feist                     output, chassis, currentProfile);
152773df0db0SJames Feist                 if (ret == CreatePIDRet::fail)
152873df0db0SJames Feist                 {
152973df0db0SJames Feist                     return;
153073df0db0SJames Feist                 }
153173df0db0SJames Feist                 else if (ret == CreatePIDRet::del)
153273df0db0SJames Feist                 {
153373df0db0SJames Feist                     continue;
153473df0db0SJames Feist                 }
153573df0db0SJames Feist 
153673df0db0SJames Feist                 if (!createNewObject)
153773df0db0SJames Feist                 {
153873df0db0SJames Feist                     for (const auto& property : output)
153973df0db0SJames Feist                     {
154073df0db0SJames Feist                         crow::connections::systemBus->async_method_call(
154173df0db0SJames Feist                             [response,
154273df0db0SJames Feist                              propertyName{std::string(property.first)}](
154373df0db0SJames Feist                                 const boost::system::error_code ec) {
154473df0db0SJames Feist                                 if (ec)
154573df0db0SJames Feist                                 {
154673df0db0SJames Feist                                     BMCWEB_LOG_ERROR << "Error patching "
154773df0db0SJames Feist                                                      << propertyName << ": "
154873df0db0SJames Feist                                                      << ec;
154973df0db0SJames Feist                                     messages::internalError(response->res);
155073df0db0SJames Feist                                     return;
155173df0db0SJames Feist                                 }
155273df0db0SJames Feist                                 messages::success(response->res);
155373df0db0SJames Feist                             },
15546ee7f774SJames Feist                             "xyz.openbmc_project.EntityManager", path,
155573df0db0SJames Feist                             "org.freedesktop.DBus.Properties", "Set", iface,
155673df0db0SJames Feist                             property.first, property.second);
155773df0db0SJames Feist                     }
155873df0db0SJames Feist                 }
155973df0db0SJames Feist                 else
156073df0db0SJames Feist                 {
156173df0db0SJames Feist                     if (chassis.empty())
156273df0db0SJames Feist                     {
156373df0db0SJames Feist                         BMCWEB_LOG_ERROR << "Failed to get chassis from config";
156473df0db0SJames Feist                         messages::invalidObject(response->res, name);
156573df0db0SJames Feist                         return;
156673df0db0SJames Feist                     }
156773df0db0SJames Feist 
156873df0db0SJames Feist                     bool foundChassis = false;
156973df0db0SJames Feist                     for (const auto& obj : managedObj)
157073df0db0SJames Feist                     {
157173df0db0SJames Feist                         if (boost::algorithm::ends_with(obj.first.str, chassis))
157273df0db0SJames Feist                         {
157373df0db0SJames Feist                             chassis = obj.first.str;
157473df0db0SJames Feist                             foundChassis = true;
157573df0db0SJames Feist                             break;
157673df0db0SJames Feist                         }
157773df0db0SJames Feist                     }
157873df0db0SJames Feist                     if (!foundChassis)
157973df0db0SJames Feist                     {
158073df0db0SJames Feist                         BMCWEB_LOG_ERROR << "Failed to find chassis on dbus";
158173df0db0SJames Feist                         messages::resourceMissingAtURI(
158273df0db0SJames Feist                             response->res, "/redfish/v1/Chassis/" + chassis);
158373df0db0SJames Feist                         return;
158473df0db0SJames Feist                     }
158573df0db0SJames Feist 
158673df0db0SJames Feist                     crow::connections::systemBus->async_method_call(
158773df0db0SJames Feist                         [response](const boost::system::error_code ec) {
158873df0db0SJames Feist                             if (ec)
158973df0db0SJames Feist                             {
159073df0db0SJames Feist                                 BMCWEB_LOG_ERROR << "Error Adding Pid Object "
159173df0db0SJames Feist                                                  << ec;
159273df0db0SJames Feist                                 messages::internalError(response->res);
159373df0db0SJames Feist                                 return;
159473df0db0SJames Feist                             }
159573df0db0SJames Feist                             messages::success(response->res);
159673df0db0SJames Feist                         },
159773df0db0SJames Feist                         "xyz.openbmc_project.EntityManager", chassis,
159873df0db0SJames Feist                         "xyz.openbmc_project.AddObject", "AddObject", output);
159973df0db0SJames Feist                 }
160073df0db0SJames Feist             }
160173df0db0SJames Feist         }
160273df0db0SJames Feist     }
160373df0db0SJames Feist     std::shared_ptr<AsyncResp> asyncResp;
160473df0db0SJames Feist     std::vector<std::pair<std::string, std::optional<nlohmann::json>>>
160573df0db0SJames Feist         configuration;
160673df0db0SJames Feist     std::optional<std::string> profile;
160773df0db0SJames Feist     dbus::utility::ManagedObjectType managedObj;
160873df0db0SJames Feist     std::vector<std::string> supportedProfiles;
160973df0db0SJames Feist     std::string currentProfile;
161073df0db0SJames Feist     std::string profileConnection;
161173df0db0SJames Feist     std::string profilePath;
161214b0b8d5SJames Feist     size_t objectCount = 0;
161373df0db0SJames Feist };
161473df0db0SJames Feist 
161573df0db0SJames Feist class Manager : public Node
161673df0db0SJames Feist {
161773df0db0SJames Feist   public:
161873df0db0SJames Feist     Manager(CrowApp& app) : Node(app, "/redfish/v1/Managers/bmc/")
161973df0db0SJames Feist     {
162073df0db0SJames Feist         uuid = app.template getMiddleware<crow::persistent_data::Middleware>()
162173df0db0SJames Feist                    .systemUuid;
162273df0db0SJames Feist         entityPrivileges = {
162373df0db0SJames Feist             {boost::beast::http::verb::get, {{"Login"}}},
162473df0db0SJames Feist             {boost::beast::http::verb::head, {{"Login"}}},
162573df0db0SJames Feist             {boost::beast::http::verb::patch, {{"ConfigureManager"}}},
162673df0db0SJames Feist             {boost::beast::http::verb::put, {{"ConfigureManager"}}},
162773df0db0SJames Feist             {boost::beast::http::verb::delete_, {{"ConfigureManager"}}},
162873df0db0SJames Feist             {boost::beast::http::verb::post, {{"ConfigureManager"}}}};
162973df0db0SJames Feist     }
163073df0db0SJames Feist 
163173df0db0SJames Feist   private:
163255c7b7a2SEd Tanous     void doGet(crow::Response& res, const crow::Request& req,
16331abe55efSEd Tanous                const std::vector<std::string>& params) override
16341abe55efSEd Tanous     {
16350f74e643SEd Tanous         res.jsonValue["@odata.id"] = "/redfish/v1/Managers/bmc";
16363e40fc74SGunnar Mills         res.jsonValue["@odata.type"] = "#Manager.v1_8_0.Manager";
16370f74e643SEd Tanous         res.jsonValue["Id"] = "bmc";
16380f74e643SEd Tanous         res.jsonValue["Name"] = "OpenBmc Manager";
16390f74e643SEd Tanous         res.jsonValue["Description"] = "Baseboard Management Controller";
16400f74e643SEd Tanous         res.jsonValue["PowerState"] = "On";
1641029573d4SEd Tanous         res.jsonValue["Status"] = {{"State", "Enabled"}, {"Health", "OK"}};
16420f74e643SEd Tanous         res.jsonValue["ManagerType"] = "BMC";
16433602e232SEd Tanous         res.jsonValue["UUID"] = systemd_utils::getUuid();
16443602e232SEd Tanous         res.jsonValue["ServiceEntryPointUUID"] = uuid;
16450f74e643SEd Tanous         res.jsonValue["Model"] = "OpenBmc"; // TODO(ed), get model
16460f74e643SEd Tanous 
16470f74e643SEd Tanous         res.jsonValue["LogServices"] = {
16480f74e643SEd Tanous             {"@odata.id", "/redfish/v1/Managers/bmc/LogServices"}};
16490f74e643SEd Tanous 
16500f74e643SEd Tanous         res.jsonValue["NetworkProtocol"] = {
16510f74e643SEd Tanous             {"@odata.id", "/redfish/v1/Managers/bmc/NetworkProtocol"}};
16520f74e643SEd Tanous 
16530f74e643SEd Tanous         res.jsonValue["EthernetInterfaces"] = {
16540f74e643SEd Tanous             {"@odata.id", "/redfish/v1/Managers/bmc/EthernetInterfaces"}};
1655107077deSPrzemyslaw Czarnowski 
1656107077deSPrzemyslaw Czarnowski #ifdef BMCWEB_ENABLE_VM_NBDPROXY
1657107077deSPrzemyslaw Czarnowski         res.jsonValue["VirtualMedia"] = {
1658107077deSPrzemyslaw Czarnowski             {"@odata.id", "/redfish/v1/Managers/bmc/VirtualMedia"}};
1659107077deSPrzemyslaw Czarnowski #endif // BMCWEB_ENABLE_VM_NBDPROXY
1660107077deSPrzemyslaw Czarnowski 
16610f74e643SEd Tanous         // default oem data
16620f74e643SEd Tanous         nlohmann::json& oem = res.jsonValue["Oem"];
16630f74e643SEd Tanous         nlohmann::json& oemOpenbmc = oem["OpenBmc"];
16640f74e643SEd Tanous         oem["@odata.type"] = "#OemManager.Oem";
16650f74e643SEd Tanous         oem["@odata.id"] = "/redfish/v1/Managers/bmc#/Oem";
16660f74e643SEd Tanous         oemOpenbmc["@odata.type"] = "#OemManager.OpenBmc";
16670f74e643SEd Tanous         oemOpenbmc["@odata.id"] = "/redfish/v1/Managers/bmc#/Oem/OpenBmc";
1668cfcd5f6bSMarri Devender Rao         oemOpenbmc["Certificates"] = {
1669cfcd5f6bSMarri Devender Rao             {"@odata.id", "/redfish/v1/Managers/bmc/Truststore/Certificates"}};
16700f74e643SEd Tanous 
16712a5c4407SGunnar Mills         // Manager.Reset (an action) can be many values, OpenBMC only supports
16722a5c4407SGunnar Mills         // BMC reboot.
16732a5c4407SGunnar Mills         nlohmann::json& managerReset =
16740f74e643SEd Tanous             res.jsonValue["Actions"]["#Manager.Reset"];
16752a5c4407SGunnar Mills         managerReset["target"] =
1676ed5befbdSJennifer Lee             "/redfish/v1/Managers/bmc/Actions/Manager.Reset";
16772a5c4407SGunnar Mills         managerReset["ResetType@Redfish.AllowableValues"] = {"GracefulRestart"};
1678ca537928SJennifer Lee 
16793e40fc74SGunnar Mills         // ResetToDefaults (Factory Reset) has values like
16803e40fc74SGunnar Mills         // PreserveNetworkAndUsers and PreserveNetwork that aren't supported
16813e40fc74SGunnar Mills         // on OpenBMC
16823e40fc74SGunnar Mills         nlohmann::json& resetToDefaults =
16833e40fc74SGunnar Mills             res.jsonValue["Actions"]["#Manager.ResetToDefaults"];
16843e40fc74SGunnar Mills         resetToDefaults["target"] =
16853e40fc74SGunnar Mills             "/redfish/v1/Managers/bmc/Actions/Manager.ResetToDefaults";
16863e40fc74SGunnar Mills         resetToDefaults["ResetType@Redfish.AllowableValues"] = {"ResetAll"};
16873e40fc74SGunnar Mills 
1688cb92c03bSAndrew Geissler         res.jsonValue["DateTime"] = crow::utility::dateTimeNow();
1689474bfad5SSantosh Puranik 
1690f8c3e6f0SKuiying Wang         // Fill in SerialConsole info
1691474bfad5SSantosh Puranik         res.jsonValue["SerialConsole"]["ServiceEnabled"] = true;
1692f8c3e6f0SKuiying Wang         res.jsonValue["SerialConsole"]["MaxConcurrentSessions"] = 15;
1693474bfad5SSantosh Puranik         res.jsonValue["SerialConsole"]["ConnectTypesSupported"] = {"IPMI",
1694474bfad5SSantosh Puranik                                                                    "SSH"};
1695ef47bb18SSantosh Puranik #ifdef BMCWEB_ENABLE_KVM
1696f8c3e6f0SKuiying Wang         // Fill in GraphicalConsole info
1697ef47bb18SSantosh Puranik         res.jsonValue["GraphicalConsole"]["ServiceEnabled"] = true;
1698704fae6cSJae Hyun Yoo         res.jsonValue["GraphicalConsole"]["MaxConcurrentSessions"] = 4;
1699ef47bb18SSantosh Puranik         res.jsonValue["GraphicalConsole"]["ConnectTypesSupported"] = {"KVMIP"};
1700ef47bb18SSantosh Puranik #endif // BMCWEB_ENABLE_KVM
1701474bfad5SSantosh Puranik 
1702603a6640SGunnar Mills         res.jsonValue["Links"]["ManagerForServers@odata.count"] = 1;
1703603a6640SGunnar Mills         res.jsonValue["Links"]["ManagerForServers"] = {
1704603a6640SGunnar Mills             {{"@odata.id", "/redfish/v1/Systems/system"}}};
170526f03899SShawn McCarney 
1706ed5befbdSJennifer Lee         std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
17075b4aa86bSJames Feist 
1708b49ac873SJames Feist         auto health = std::make_shared<HealthPopulate>(asyncResp);
1709b49ac873SJames Feist         health->isManagersHealth = true;
1710b49ac873SJames Feist         health->populate();
1711b49ac873SJames Feist 
1712e90c5052SAndrew Geissler         fw_util::getActiveFwVersion(asyncResp, fw_util::bmcPurpose,
1713e90c5052SAndrew Geissler                                     "FirmwareVersion");
17140f6b00bdSJames Feist 
171573df0db0SJames Feist         auto pids = std::make_shared<GetPIDValues>(asyncResp);
171673df0db0SJames Feist         pids->run();
1717c5d03ff4SJennifer Lee 
1718c5d03ff4SJennifer Lee         getMainChassisId(asyncResp, [](const std::string& chassisId,
1719c5d03ff4SJennifer Lee                                        const std::shared_ptr<AsyncResp> aRsp) {
1720c5d03ff4SJennifer Lee             aRsp->res.jsonValue["Links"]["ManagerForChassis@odata.count"] = 1;
1721c5d03ff4SJennifer Lee             aRsp->res.jsonValue["Links"]["ManagerForChassis"] = {
1722c5d03ff4SJennifer Lee                 {{"@odata.id", "/redfish/v1/Chassis/" + chassisId}}};
17232c0feb00SJason M. Bills             aRsp->res.jsonValue["Links"]["ManagerInChassis"] = {
17242c0feb00SJason M. Bills                 {"@odata.id", "/redfish/v1/Chassis/" + chassisId}};
1725c5d03ff4SJennifer Lee         });
17260f6b00bdSJames Feist 
17270f6b00bdSJames Feist         static bool started = false;
17280f6b00bdSJames Feist 
17290f6b00bdSJames Feist         if (!started)
17300f6b00bdSJames Feist         {
17310f6b00bdSJames Feist             crow::connections::systemBus->async_method_call(
17320f6b00bdSJames Feist                 [asyncResp](const boost::system::error_code ec,
17330f6b00bdSJames Feist                             const std::variant<double>& resp) {
17340f6b00bdSJames Feist                     if (ec)
17350f6b00bdSJames Feist                     {
17360f6b00bdSJames Feist                         BMCWEB_LOG_ERROR << "Error while getting progress";
17370f6b00bdSJames Feist                         messages::internalError(asyncResp->res);
17380f6b00bdSJames Feist                         return;
17390f6b00bdSJames Feist                     }
17400f6b00bdSJames Feist                     const double* val = std::get_if<double>(&resp);
17410f6b00bdSJames Feist                     if (val == nullptr)
17420f6b00bdSJames Feist                     {
17430f6b00bdSJames Feist                         BMCWEB_LOG_ERROR
17440f6b00bdSJames Feist                             << "Invalid response while getting progress";
17450f6b00bdSJames Feist                         messages::internalError(asyncResp->res);
17460f6b00bdSJames Feist                         return;
17470f6b00bdSJames Feist                     }
17480f6b00bdSJames Feist                     if (*val < 1.0)
17490f6b00bdSJames Feist                     {
17500f6b00bdSJames Feist                         asyncResp->res.jsonValue["Status"]["State"] =
17510f6b00bdSJames Feist                             "Starting";
17520f6b00bdSJames Feist                         started = true;
17530f6b00bdSJames Feist                     }
17540f6b00bdSJames Feist                 },
17550f6b00bdSJames Feist                 "org.freedesktop.systemd1", "/org/freedesktop/systemd1",
17560f6b00bdSJames Feist                 "org.freedesktop.DBus.Properties", "Get",
17570f6b00bdSJames Feist                 "org.freedesktop.systemd1.Manager", "Progress");
17580f6b00bdSJames Feist         }
175983ff9ab6SJames Feist     }
17605b4aa86bSJames Feist 
17615b4aa86bSJames Feist     void doPatch(crow::Response& res, const crow::Request& req,
17625b4aa86bSJames Feist                  const std::vector<std::string>& params) override
17635b4aa86bSJames Feist     {
17640627a2c7SEd Tanous         std::optional<nlohmann::json> oem;
1765af5d6058SSantosh Puranik         std::optional<std::string> datetime;
176641352c24SSantosh Puranik         std::shared_ptr<AsyncResp> response = std::make_shared<AsyncResp>(res);
17670627a2c7SEd Tanous 
176841352c24SSantosh Puranik         if (!json_util::readJson(req, response->res, "Oem", oem, "DateTime",
176941352c24SSantosh Puranik                                  datetime))
177083ff9ab6SJames Feist         {
177183ff9ab6SJames Feist             return;
177283ff9ab6SJames Feist         }
17730627a2c7SEd Tanous 
17740627a2c7SEd Tanous         if (oem)
177583ff9ab6SJames Feist         {
17765f2caaefSJames Feist             std::optional<nlohmann::json> openbmc;
177743b761d0SEd Tanous             if (!redfish::json_util::readJson(*oem, res, "OpenBmc", openbmc))
177883ff9ab6SJames Feist             {
177943b761d0SEd Tanous                 BMCWEB_LOG_ERROR << "Line:" << __LINE__ << ", Illegal Property "
178043b761d0SEd Tanous                                  << oem->dump();
178183ff9ab6SJames Feist                 return;
178283ff9ab6SJames Feist             }
17835f2caaefSJames Feist             if (openbmc)
178483ff9ab6SJames Feist             {
17855f2caaefSJames Feist                 std::optional<nlohmann::json> fan;
178643b761d0SEd Tanous                 if (!redfish::json_util::readJson(*openbmc, res, "Fan", fan))
178783ff9ab6SJames Feist                 {
17885f2caaefSJames Feist                     BMCWEB_LOG_ERROR << "Line:" << __LINE__
17895f2caaefSJames Feist                                      << ", Illegal Property "
17905f2caaefSJames Feist                                      << openbmc->dump();
179183ff9ab6SJames Feist                     return;
179283ff9ab6SJames Feist                 }
17935f2caaefSJames Feist                 if (fan)
179483ff9ab6SJames Feist                 {
179573df0db0SJames Feist                     auto pid = std::make_shared<SetPIDValues>(response, *fan);
179673df0db0SJames Feist                     pid->run();
179783ff9ab6SJames Feist                 }
179883ff9ab6SJames Feist             }
179983ff9ab6SJames Feist         }
1800af5d6058SSantosh Puranik         if (datetime)
1801af5d6058SSantosh Puranik         {
1802af5d6058SSantosh Puranik             setDateTime(response, std::move(*datetime));
1803af5d6058SSantosh Puranik         }
1804af5d6058SSantosh Puranik     }
1805af5d6058SSantosh Puranik 
1806af5d6058SSantosh Puranik     void setDateTime(std::shared_ptr<AsyncResp> aResp,
1807af5d6058SSantosh Puranik                      std::string datetime) const
1808af5d6058SSantosh Puranik     {
1809af5d6058SSantosh Puranik         BMCWEB_LOG_DEBUG << "Set date time: " << datetime;
1810af5d6058SSantosh Puranik 
1811af5d6058SSantosh Puranik         std::stringstream stream(datetime);
1812af5d6058SSantosh Puranik         // Convert from ISO 8601 to boost local_time
1813af5d6058SSantosh Puranik         // (BMC only has time in UTC)
1814af5d6058SSantosh Puranik         boost::posix_time::ptime posixTime;
1815af5d6058SSantosh Puranik         boost::posix_time::ptime epoch(boost::gregorian::date(1970, 1, 1));
1816af5d6058SSantosh Puranik         // Facet gets deleted with the stringsteam
1817af5d6058SSantosh Puranik         auto ifc = std::make_unique<boost::local_time::local_time_input_facet>(
1818af5d6058SSantosh Puranik             "%Y-%m-%d %H:%M:%S%F %ZP");
1819af5d6058SSantosh Puranik         stream.imbue(std::locale(stream.getloc(), ifc.release()));
1820af5d6058SSantosh Puranik 
1821af5d6058SSantosh Puranik         boost::local_time::local_date_time ldt(
1822af5d6058SSantosh Puranik             boost::local_time::not_a_date_time);
1823af5d6058SSantosh Puranik 
1824af5d6058SSantosh Puranik         if (stream >> ldt)
1825af5d6058SSantosh Puranik         {
1826af5d6058SSantosh Puranik             posixTime = ldt.utc_time();
1827af5d6058SSantosh Puranik             boost::posix_time::time_duration dur = posixTime - epoch;
1828af5d6058SSantosh Puranik             uint64_t durMicroSecs =
1829af5d6058SSantosh Puranik                 static_cast<uint64_t>(dur.total_microseconds());
1830af5d6058SSantosh Puranik             crow::connections::systemBus->async_method_call(
1831af5d6058SSantosh Puranik                 [aResp{std::move(aResp)}, datetime{std::move(datetime)}](
1832af5d6058SSantosh Puranik                     const boost::system::error_code ec) {
1833af5d6058SSantosh Puranik                     if (ec)
1834af5d6058SSantosh Puranik                     {
1835af5d6058SSantosh Puranik                         BMCWEB_LOG_DEBUG << "Failed to set elapsed time. "
1836af5d6058SSantosh Puranik                                             "DBUS response error "
1837af5d6058SSantosh Puranik                                          << ec;
1838af5d6058SSantosh Puranik                         messages::internalError(aResp->res);
1839af5d6058SSantosh Puranik                         return;
1840af5d6058SSantosh Puranik                     }
1841af5d6058SSantosh Puranik                     aResp->res.jsonValue["DateTime"] = datetime;
1842af5d6058SSantosh Puranik                 },
1843af5d6058SSantosh Puranik                 "xyz.openbmc_project.Time.Manager",
1844af5d6058SSantosh Puranik                 "/xyz/openbmc_project/time/bmc",
1845af5d6058SSantosh Puranik                 "org.freedesktop.DBus.Properties", "Set",
1846af5d6058SSantosh Puranik                 "xyz.openbmc_project.Time.EpochTime", "Elapsed",
1847af5d6058SSantosh Puranik                 std::variant<uint64_t>(durMicroSecs));
1848af5d6058SSantosh Puranik         }
1849af5d6058SSantosh Puranik         else
1850af5d6058SSantosh Puranik         {
1851af5d6058SSantosh Puranik             messages::propertyValueFormatError(aResp->res, datetime,
1852af5d6058SSantosh Puranik                                                "DateTime");
1853af5d6058SSantosh Puranik             return;
1854af5d6058SSantosh Puranik         }
185583ff9ab6SJames Feist     }
18569c310685SBorawski.Lukasz 
18570f74e643SEd Tanous     std::string uuid;
18589c310685SBorawski.Lukasz };
18599c310685SBorawski.Lukasz 
18601abe55efSEd Tanous class ManagerCollection : public Node
18611abe55efSEd Tanous {
18629c310685SBorawski.Lukasz   public:
18631abe55efSEd Tanous     ManagerCollection(CrowApp& app) : Node(app, "/redfish/v1/Managers/")
18641abe55efSEd Tanous     {
1865a434f2bdSEd Tanous         entityPrivileges = {
1866a434f2bdSEd Tanous             {boost::beast::http::verb::get, {{"Login"}}},
1867e0d918bcSEd Tanous             {boost::beast::http::verb::head, {{"Login"}}},
1868e0d918bcSEd Tanous             {boost::beast::http::verb::patch, {{"ConfigureManager"}}},
1869e0d918bcSEd Tanous             {boost::beast::http::verb::put, {{"ConfigureManager"}}},
1870e0d918bcSEd Tanous             {boost::beast::http::verb::delete_, {{"ConfigureManager"}}},
1871e0d918bcSEd Tanous             {boost::beast::http::verb::post, {{"ConfigureManager"}}}};
18729c310685SBorawski.Lukasz     }
18739c310685SBorawski.Lukasz 
18749c310685SBorawski.Lukasz   private:
187555c7b7a2SEd Tanous     void doGet(crow::Response& res, const crow::Request& req,
18761abe55efSEd Tanous                const std::vector<std::string>& params) override
18771abe55efSEd Tanous     {
187883ff9ab6SJames Feist         // Collections don't include the static data added by SubRoute
187983ff9ab6SJames Feist         // because it has a duplicate entry for members
188055c7b7a2SEd Tanous         res.jsonValue["@odata.id"] = "/redfish/v1/Managers";
188155c7b7a2SEd Tanous         res.jsonValue["@odata.type"] = "#ManagerCollection.ManagerCollection";
188255c7b7a2SEd Tanous         res.jsonValue["Name"] = "Manager Collection";
188355c7b7a2SEd Tanous         res.jsonValue["Members@odata.count"] = 1;
188455c7b7a2SEd Tanous         res.jsonValue["Members"] = {
18855b4aa86bSJames Feist             {{"@odata.id", "/redfish/v1/Managers/bmc"}}};
18869c310685SBorawski.Lukasz         res.end();
18879c310685SBorawski.Lukasz     }
18889c310685SBorawski.Lukasz };
18899c310685SBorawski.Lukasz } // namespace redfish
1890