xref: /openbmc/bmcweb/features/redfish/lib/managers.hpp (revision 6ee7f774bf6393d48c7215e02d6dbf00322bc9f5)
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>
2573df0db0SJames Feist #include <memory>
26af5d6058SSantosh Puranik #include <sstream>
27e90c5052SAndrew Geissler #include <utils/fw_utils.hpp>
287bffdb7eSBernard Wong #include <utils/systemd_utils.hpp>
29abf2add6SEd Tanous #include <variant>
305b4aa86bSJames Feist 
311abe55efSEd Tanous namespace redfish
321abe55efSEd Tanous {
33ed5befbdSJennifer Lee 
34ed5befbdSJennifer Lee /**
35ed5befbdSJennifer Lee  * ManagerActionsReset class supports handle POST method for Reset action.
36ed5befbdSJennifer Lee  * The class retrieves and sends data directly to dbus.
37ed5befbdSJennifer Lee  */
38ed5befbdSJennifer Lee class ManagerActionsReset : public Node
39ed5befbdSJennifer Lee {
40ed5befbdSJennifer Lee   public:
41ed5befbdSJennifer Lee     ManagerActionsReset(CrowApp& app) :
42ed5befbdSJennifer Lee         Node(app, "/redfish/v1/Managers/bmc/Actions/Manager.Reset/")
43ed5befbdSJennifer Lee     {
44ed5befbdSJennifer Lee         entityPrivileges = {
45ed5befbdSJennifer Lee             {boost::beast::http::verb::get, {{"Login"}}},
46ed5befbdSJennifer Lee             {boost::beast::http::verb::head, {{"Login"}}},
47ed5befbdSJennifer Lee             {boost::beast::http::verb::patch, {{"ConfigureManager"}}},
48ed5befbdSJennifer Lee             {boost::beast::http::verb::put, {{"ConfigureManager"}}},
49ed5befbdSJennifer Lee             {boost::beast::http::verb::delete_, {{"ConfigureManager"}}},
50ed5befbdSJennifer Lee             {boost::beast::http::verb::post, {{"ConfigureManager"}}}};
51ed5befbdSJennifer Lee     }
52ed5befbdSJennifer Lee 
53ed5befbdSJennifer Lee   private:
54ed5befbdSJennifer Lee     /**
55ed5befbdSJennifer Lee      * Function handles POST method request.
56ed5befbdSJennifer Lee      * Analyzes POST body message before sends Reset request data to dbus.
57ed5befbdSJennifer Lee      * OpenBMC allows for ResetType is GracefulRestart only.
58ed5befbdSJennifer Lee      */
59ed5befbdSJennifer Lee     void doPost(crow::Response& res, const crow::Request& req,
60ed5befbdSJennifer Lee                 const std::vector<std::string>& params) override
61ed5befbdSJennifer Lee     {
62ed5befbdSJennifer Lee         std::string resetType;
63ed5befbdSJennifer Lee 
64ed5befbdSJennifer Lee         if (!json_util::readJson(req, res, "ResetType", resetType))
65ed5befbdSJennifer Lee         {
66ed5befbdSJennifer Lee             return;
67ed5befbdSJennifer Lee         }
68ed5befbdSJennifer Lee 
69ed5befbdSJennifer Lee         if (resetType != "GracefulRestart")
70ed5befbdSJennifer Lee         {
71ed5befbdSJennifer Lee             res.result(boost::beast::http::status::bad_request);
72ed5befbdSJennifer Lee             messages::actionParameterNotSupported(res, resetType, "ResetType");
73ed5befbdSJennifer Lee             BMCWEB_LOG_ERROR << "Request incorrect action parameter: "
74ed5befbdSJennifer Lee                              << resetType;
75ed5befbdSJennifer Lee             res.end();
76ed5befbdSJennifer Lee             return;
77ed5befbdSJennifer Lee         }
78ed5befbdSJennifer Lee         doBMCGracefulRestart(res, req, params);
79ed5befbdSJennifer Lee     }
80ed5befbdSJennifer Lee 
81ed5befbdSJennifer Lee     /**
82ed5befbdSJennifer Lee      * Function transceives data with dbus directly.
83ed5befbdSJennifer Lee      * All BMC state properties will be retrieved before sending reset request.
84ed5befbdSJennifer Lee      */
85ed5befbdSJennifer Lee     void doBMCGracefulRestart(crow::Response& res, const crow::Request& req,
86ed5befbdSJennifer Lee                               const std::vector<std::string>& params)
87ed5befbdSJennifer Lee     {
88ed5befbdSJennifer Lee         const char* processName = "xyz.openbmc_project.State.BMC";
89ed5befbdSJennifer Lee         const char* objectPath = "/xyz/openbmc_project/state/bmc0";
90ed5befbdSJennifer Lee         const char* interfaceName = "xyz.openbmc_project.State.BMC";
91ed5befbdSJennifer Lee         const std::string& propertyValue =
92ed5befbdSJennifer Lee             "xyz.openbmc_project.State.BMC.Transition.Reboot";
93ed5befbdSJennifer Lee         const char* destProperty = "RequestedBMCTransition";
94ed5befbdSJennifer Lee 
95ed5befbdSJennifer Lee         // Create the D-Bus variant for D-Bus call.
96ed5befbdSJennifer Lee         VariantType dbusPropertyValue(propertyValue);
97ed5befbdSJennifer Lee 
98ed5befbdSJennifer Lee         std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
99ed5befbdSJennifer Lee 
100ed5befbdSJennifer Lee         crow::connections::systemBus->async_method_call(
101ed5befbdSJennifer Lee             [asyncResp](const boost::system::error_code ec) {
102ed5befbdSJennifer Lee                 // Use "Set" method to set the property value.
103ed5befbdSJennifer Lee                 if (ec)
104ed5befbdSJennifer Lee                 {
105ed5befbdSJennifer Lee                     BMCWEB_LOG_ERROR << "[Set] Bad D-Bus request error: " << ec;
106ed5befbdSJennifer Lee                     messages::internalError(asyncResp->res);
107ed5befbdSJennifer Lee                     return;
108ed5befbdSJennifer Lee                 }
109ed5befbdSJennifer Lee 
110ed5befbdSJennifer Lee                 messages::success(asyncResp->res);
111ed5befbdSJennifer Lee             },
112ed5befbdSJennifer Lee             processName, objectPath, "org.freedesktop.DBus.Properties", "Set",
113ed5befbdSJennifer Lee             interfaceName, destProperty, dbusPropertyValue);
114ed5befbdSJennifer Lee     }
115ed5befbdSJennifer Lee };
116ed5befbdSJennifer Lee 
1175b4aa86bSJames Feist static constexpr const char* objectManagerIface =
1185b4aa86bSJames Feist     "org.freedesktop.DBus.ObjectManager";
1195b4aa86bSJames Feist static constexpr const char* pidConfigurationIface =
1205b4aa86bSJames Feist     "xyz.openbmc_project.Configuration.Pid";
1215b4aa86bSJames Feist static constexpr const char* pidZoneConfigurationIface =
1225b4aa86bSJames Feist     "xyz.openbmc_project.Configuration.Pid.Zone";
123b7a08d04SJames Feist static constexpr const char* stepwiseConfigurationIface =
124b7a08d04SJames Feist     "xyz.openbmc_project.Configuration.Stepwise";
12573df0db0SJames Feist static constexpr const char* thermalModeIface =
12673df0db0SJames Feist     "xyz.openbmc_project.Control.ThermalMode";
1279c310685SBorawski.Lukasz 
1285b4aa86bSJames Feist static void asyncPopulatePid(const std::string& connection,
1295b4aa86bSJames Feist                              const std::string& path,
13073df0db0SJames Feist                              const std::string& currentProfile,
13173df0db0SJames Feist                              const std::vector<std::string>& supportedProfiles,
1325b4aa86bSJames Feist                              std::shared_ptr<AsyncResp> asyncResp)
1335b4aa86bSJames Feist {
1345b4aa86bSJames Feist 
1355b4aa86bSJames Feist     crow::connections::systemBus->async_method_call(
13673df0db0SJames Feist         [asyncResp, currentProfile, supportedProfiles](
13773df0db0SJames Feist             const boost::system::error_code ec,
1385b4aa86bSJames Feist             const dbus::utility::ManagedObjectType& managedObj) {
1395b4aa86bSJames Feist             if (ec)
1405b4aa86bSJames Feist             {
1415b4aa86bSJames Feist                 BMCWEB_LOG_ERROR << ec;
1425b4aa86bSJames Feist                 asyncResp->res.jsonValue.clear();
143f12894f8SJason M. Bills                 messages::internalError(asyncResp->res);
1445b4aa86bSJames Feist                 return;
1455b4aa86bSJames Feist             }
1465b4aa86bSJames Feist             nlohmann::json& configRoot =
1475b4aa86bSJames Feist                 asyncResp->res.jsonValue["Oem"]["OpenBmc"]["Fan"];
1485b4aa86bSJames Feist             nlohmann::json& fans = configRoot["FanControllers"];
1495b4aa86bSJames Feist             fans["@odata.type"] = "#OemManager.FanControllers";
1505b4aa86bSJames Feist             fans["@odata.context"] =
1515b4aa86bSJames Feist                 "/redfish/v1/$metadata#OemManager.FanControllers";
1525b4aa86bSJames Feist             fans["@odata.id"] = "/redfish/v1/Managers/bmc#/Oem/OpenBmc/"
1535b4aa86bSJames Feist                                 "Fan/FanControllers";
1545b4aa86bSJames Feist 
1555b4aa86bSJames Feist             nlohmann::json& pids = configRoot["PidControllers"];
1565b4aa86bSJames Feist             pids["@odata.type"] = "#OemManager.PidControllers";
1575b4aa86bSJames Feist             pids["@odata.context"] =
1585b4aa86bSJames Feist                 "/redfish/v1/$metadata#OemManager.PidControllers";
1595b4aa86bSJames Feist             pids["@odata.id"] =
1605b4aa86bSJames Feist                 "/redfish/v1/Managers/bmc#/Oem/OpenBmc/Fan/PidControllers";
1615b4aa86bSJames Feist 
162b7a08d04SJames Feist             nlohmann::json& stepwise = configRoot["StepwiseControllers"];
163b7a08d04SJames Feist             stepwise["@odata.type"] = "#OemManager.StepwiseControllers";
164b7a08d04SJames Feist             stepwise["@odata.context"] =
165b7a08d04SJames Feist                 "/redfish/v1/$metadata#OemManager.StepwiseControllers";
166b7a08d04SJames Feist             stepwise["@odata.id"] =
167b7a08d04SJames Feist                 "/redfish/v1/Managers/bmc#/Oem/OpenBmc/Fan/StepwiseControllers";
168b7a08d04SJames Feist 
1695b4aa86bSJames Feist             nlohmann::json& zones = configRoot["FanZones"];
1705b4aa86bSJames Feist             zones["@odata.id"] =
1715b4aa86bSJames Feist                 "/redfish/v1/Managers/bmc#/Oem/OpenBmc/Fan/FanZones";
1725b4aa86bSJames Feist             zones["@odata.type"] = "#OemManager.FanZones";
1735b4aa86bSJames Feist             zones["@odata.context"] =
1745b4aa86bSJames Feist                 "/redfish/v1/$metadata#OemManager.FanZones";
1755b4aa86bSJames Feist             configRoot["@odata.id"] =
1765b4aa86bSJames Feist                 "/redfish/v1/Managers/bmc#/Oem/OpenBmc/Fan";
1775b4aa86bSJames Feist             configRoot["@odata.type"] = "#OemManager.Fan";
1785b4aa86bSJames Feist             configRoot["@odata.context"] =
1795b4aa86bSJames Feist                 "/redfish/v1/$metadata#OemManager.Fan";
18073df0db0SJames Feist             configRoot["Profile@Redfish.AllowableValues"] = supportedProfiles;
18173df0db0SJames Feist 
18273df0db0SJames Feist             if (!currentProfile.empty())
18373df0db0SJames Feist             {
18473df0db0SJames Feist                 configRoot["Profile"] = currentProfile;
18573df0db0SJames Feist             }
18673df0db0SJames Feist             BMCWEB_LOG_ERROR << "profile = " << currentProfile << " !";
1875b4aa86bSJames Feist 
1885b4aa86bSJames Feist             for (const auto& pathPair : managedObj)
1895b4aa86bSJames Feist             {
1905b4aa86bSJames Feist                 for (const auto& intfPair : pathPair.second)
1915b4aa86bSJames Feist                 {
1925b4aa86bSJames Feist                     if (intfPair.first != pidConfigurationIface &&
193b7a08d04SJames Feist                         intfPair.first != pidZoneConfigurationIface &&
194b7a08d04SJames Feist                         intfPair.first != stepwiseConfigurationIface)
1955b4aa86bSJames Feist                     {
1965b4aa86bSJames Feist                         continue;
1975b4aa86bSJames Feist                     }
1985b4aa86bSJames Feist                     auto findName = intfPair.second.find("Name");
1995b4aa86bSJames Feist                     if (findName == intfPair.second.end())
2005b4aa86bSJames Feist                     {
2015b4aa86bSJames Feist                         BMCWEB_LOG_ERROR << "Pid Field missing Name";
202a08b46ccSJason M. Bills                         messages::internalError(asyncResp->res);
2035b4aa86bSJames Feist                         return;
2045b4aa86bSJames Feist                     }
20573df0db0SJames Feist 
2065b4aa86bSJames Feist                     const std::string* namePtr =
207abf2add6SEd Tanous                         std::get_if<std::string>(&findName->second);
2085b4aa86bSJames Feist                     if (namePtr == nullptr)
2095b4aa86bSJames Feist                     {
2105b4aa86bSJames Feist                         BMCWEB_LOG_ERROR << "Pid Name Field illegal";
211b7a08d04SJames Feist                         messages::internalError(asyncResp->res);
2125b4aa86bSJames Feist                         return;
2135b4aa86bSJames Feist                     }
2145b4aa86bSJames Feist                     std::string name = *namePtr;
2155b4aa86bSJames Feist                     dbus::utility::escapePathForDbus(name);
21673df0db0SJames Feist 
21773df0db0SJames Feist                     auto findProfiles = intfPair.second.find("Profiles");
21873df0db0SJames Feist                     if (findProfiles != intfPair.second.end())
21973df0db0SJames Feist                     {
22073df0db0SJames Feist                         const std::vector<std::string>* profiles =
22173df0db0SJames Feist                             std::get_if<std::vector<std::string>>(
22273df0db0SJames Feist                                 &findProfiles->second);
22373df0db0SJames Feist                         if (profiles == nullptr)
22473df0db0SJames Feist                         {
22573df0db0SJames Feist                             BMCWEB_LOG_ERROR << "Pid Profiles Field illegal";
22673df0db0SJames Feist                             messages::internalError(asyncResp->res);
22773df0db0SJames Feist                             return;
22873df0db0SJames Feist                         }
22973df0db0SJames Feist                         if (std::find(profiles->begin(), profiles->end(),
23073df0db0SJames Feist                                       currentProfile) == profiles->end())
23173df0db0SJames Feist                         {
23273df0db0SJames Feist                             BMCWEB_LOG_INFO
23373df0db0SJames Feist                                 << name << " not supported in current profile";
23473df0db0SJames Feist                             continue;
23573df0db0SJames Feist                         }
23673df0db0SJames Feist                     }
237b7a08d04SJames Feist                     nlohmann::json* config = nullptr;
238c33a90ecSJames Feist 
239c33a90ecSJames Feist                     const std::string* classPtr = nullptr;
240c33a90ecSJames Feist                     auto findClass = intfPair.second.find("Class");
241c33a90ecSJames Feist                     if (findClass != intfPair.second.end())
242c33a90ecSJames Feist                     {
243c33a90ecSJames Feist                         classPtr = std::get_if<std::string>(&findClass->second);
244c33a90ecSJames Feist                     }
245c33a90ecSJames Feist 
2465b4aa86bSJames Feist                     if (intfPair.first == pidZoneConfigurationIface)
2475b4aa86bSJames Feist                     {
2485b4aa86bSJames Feist                         std::string chassis;
2495b4aa86bSJames Feist                         if (!dbus::utility::getNthStringFromPath(
2505b4aa86bSJames Feist                                 pathPair.first.str, 5, chassis))
2515b4aa86bSJames Feist                         {
2525b4aa86bSJames Feist                             chassis = "#IllegalValue";
2535b4aa86bSJames Feist                         }
2545b4aa86bSJames Feist                         nlohmann::json& zone = zones[name];
2555b4aa86bSJames Feist                         zone["Chassis"] = {
2565b4aa86bSJames Feist                             {"@odata.id", "/redfish/v1/Chassis/" + chassis}};
2575b4aa86bSJames Feist                         zone["@odata.id"] = "/redfish/v1/Managers/bmc#/Oem/"
2585b4aa86bSJames Feist                                             "OpenBmc/Fan/FanZones/" +
2595b4aa86bSJames Feist                                             name;
2605b4aa86bSJames Feist                         zone["@odata.type"] = "#OemManager.FanZone";
2615b4aa86bSJames Feist                         zone["@odata.context"] =
2625b4aa86bSJames Feist                             "/redfish/v1/$metadata#OemManager.FanZone";
263b7a08d04SJames Feist                         config = &zone;
2645b4aa86bSJames Feist                     }
2655b4aa86bSJames Feist 
266b7a08d04SJames Feist                     else if (intfPair.first == stepwiseConfigurationIface)
2675b4aa86bSJames Feist                     {
268c33a90ecSJames Feist                         if (classPtr == nullptr)
269c33a90ecSJames Feist                         {
270c33a90ecSJames Feist                             BMCWEB_LOG_ERROR << "Pid Class Field illegal";
271c33a90ecSJames Feist                             messages::internalError(asyncResp->res);
272c33a90ecSJames Feist                             return;
273c33a90ecSJames Feist                         }
274c33a90ecSJames Feist 
275b7a08d04SJames Feist                         nlohmann::json& controller = stepwise[name];
276b7a08d04SJames Feist                         config = &controller;
2775b4aa86bSJames Feist 
278b7a08d04SJames Feist                         controller["@odata.id"] =
279b7a08d04SJames Feist                             "/redfish/v1/Managers/bmc#/Oem/"
280b7a08d04SJames Feist                             "OpenBmc/Fan/StepwiseControllers/" +
281271584abSEd Tanous                             name;
282b7a08d04SJames Feist                         controller["@odata.type"] =
283b7a08d04SJames Feist                             "#OemManager.StepwiseController";
284b7a08d04SJames Feist 
285b7a08d04SJames Feist                         controller["@odata.context"] =
286b7a08d04SJames Feist                             "/redfish/v1/"
287b7a08d04SJames Feist                             "$metadata#OemManager.StepwiseController";
288c33a90ecSJames Feist                         controller["Direction"] = *classPtr;
2895b4aa86bSJames Feist                     }
2905b4aa86bSJames Feist 
2915b4aa86bSJames Feist                     // pid and fans are off the same configuration
292b7a08d04SJames Feist                     else if (intfPair.first == pidConfigurationIface)
2935b4aa86bSJames Feist                     {
294c33a90ecSJames Feist 
2955b4aa86bSJames Feist                         if (classPtr == nullptr)
2965b4aa86bSJames Feist                         {
2975b4aa86bSJames Feist                             BMCWEB_LOG_ERROR << "Pid Class Field illegal";
298a08b46ccSJason M. Bills                             messages::internalError(asyncResp->res);
2995b4aa86bSJames Feist                             return;
3005b4aa86bSJames Feist                         }
3015b4aa86bSJames Feist                         bool isFan = *classPtr == "fan";
3025b4aa86bSJames Feist                         nlohmann::json& element =
3035b4aa86bSJames Feist                             isFan ? fans[name] : pids[name];
304b7a08d04SJames Feist                         config = &element;
3055b4aa86bSJames Feist                         if (isFan)
3065b4aa86bSJames Feist                         {
3075b4aa86bSJames Feist                             element["@odata.id"] =
3085b4aa86bSJames Feist                                 "/redfish/v1/Managers/bmc#/Oem/"
3095b4aa86bSJames Feist                                 "OpenBmc/Fan/FanControllers/" +
310271584abSEd Tanous                                 name;
3115b4aa86bSJames Feist                             element["@odata.type"] =
3125b4aa86bSJames Feist                                 "#OemManager.FanController";
3135b4aa86bSJames Feist 
3145b4aa86bSJames Feist                             element["@odata.context"] =
3155b4aa86bSJames Feist                                 "/redfish/v1/"
3165b4aa86bSJames Feist                                 "$metadata#OemManager.FanController";
3175b4aa86bSJames Feist                         }
3185b4aa86bSJames Feist                         else
3195b4aa86bSJames Feist                         {
3205b4aa86bSJames Feist                             element["@odata.id"] =
3215b4aa86bSJames Feist                                 "/redfish/v1/Managers/bmc#/Oem/"
3225b4aa86bSJames Feist                                 "OpenBmc/Fan/PidControllers/" +
323271584abSEd Tanous                                 name;
3245b4aa86bSJames Feist                             element["@odata.type"] =
3255b4aa86bSJames Feist                                 "#OemManager.PidController";
3265b4aa86bSJames Feist                             element["@odata.context"] =
3275b4aa86bSJames Feist                                 "/redfish/v1/$metadata"
3285b4aa86bSJames Feist                                 "#OemManager.PidController";
3295b4aa86bSJames Feist                         }
330b7a08d04SJames Feist                     }
331b7a08d04SJames Feist                     else
332b7a08d04SJames Feist                     {
333b7a08d04SJames Feist                         BMCWEB_LOG_ERROR << "Unexpected configuration";
334b7a08d04SJames Feist                         messages::internalError(asyncResp->res);
335b7a08d04SJames Feist                         return;
336b7a08d04SJames Feist                     }
337b7a08d04SJames Feist 
338b7a08d04SJames Feist                     // used for making maps out of 2 vectors
339b7a08d04SJames Feist                     const std::vector<double>* keys = nullptr;
340b7a08d04SJames Feist                     const std::vector<double>* values = nullptr;
341b7a08d04SJames Feist 
342b7a08d04SJames Feist                     for (const auto& propertyPair : intfPair.second)
343b7a08d04SJames Feist                     {
344b7a08d04SJames Feist                         if (propertyPair.first == "Type" ||
345b7a08d04SJames Feist                             propertyPair.first == "Class" ||
346b7a08d04SJames Feist                             propertyPair.first == "Name")
347b7a08d04SJames Feist                         {
348b7a08d04SJames Feist                             continue;
349b7a08d04SJames Feist                         }
350b7a08d04SJames Feist 
351b7a08d04SJames Feist                         // zones
352b7a08d04SJames Feist                         if (intfPair.first == pidZoneConfigurationIface)
353b7a08d04SJames Feist                         {
354b7a08d04SJames Feist                             const double* ptr =
355abf2add6SEd Tanous                                 std::get_if<double>(&propertyPair.second);
356b7a08d04SJames Feist                             if (ptr == nullptr)
357b7a08d04SJames Feist                             {
358b7a08d04SJames Feist                                 BMCWEB_LOG_ERROR << "Field Illegal "
359b7a08d04SJames Feist                                                  << propertyPair.first;
360b7a08d04SJames Feist                                 messages::internalError(asyncResp->res);
361b7a08d04SJames Feist                                 return;
362b7a08d04SJames Feist                             }
363b7a08d04SJames Feist                             (*config)[propertyPair.first] = *ptr;
364b7a08d04SJames Feist                         }
365b7a08d04SJames Feist 
366b7a08d04SJames Feist                         if (intfPair.first == stepwiseConfigurationIface)
367b7a08d04SJames Feist                         {
368b7a08d04SJames Feist                             if (propertyPair.first == "Reading" ||
369b7a08d04SJames Feist                                 propertyPair.first == "Output")
370b7a08d04SJames Feist                             {
371b7a08d04SJames Feist                                 const std::vector<double>* ptr =
372abf2add6SEd Tanous                                     std::get_if<std::vector<double>>(
373b7a08d04SJames Feist                                         &propertyPair.second);
374b7a08d04SJames Feist 
375b7a08d04SJames Feist                                 if (ptr == nullptr)
376b7a08d04SJames Feist                                 {
377b7a08d04SJames Feist                                     BMCWEB_LOG_ERROR << "Field Illegal "
378b7a08d04SJames Feist                                                      << propertyPair.first;
379b7a08d04SJames Feist                                     messages::internalError(asyncResp->res);
380b7a08d04SJames Feist                                     return;
381b7a08d04SJames Feist                                 }
382b7a08d04SJames Feist 
383b7a08d04SJames Feist                                 if (propertyPair.first == "Reading")
384b7a08d04SJames Feist                                 {
385b7a08d04SJames Feist                                     keys = ptr;
386b7a08d04SJames Feist                                 }
387b7a08d04SJames Feist                                 else
388b7a08d04SJames Feist                                 {
389b7a08d04SJames Feist                                     values = ptr;
390b7a08d04SJames Feist                                 }
391b7a08d04SJames Feist                                 if (keys && values)
392b7a08d04SJames Feist                                 {
393b7a08d04SJames Feist                                     if (keys->size() != values->size())
394b7a08d04SJames Feist                                     {
395b7a08d04SJames Feist                                         BMCWEB_LOG_ERROR
396b7a08d04SJames Feist                                             << "Reading and Output size don't "
397b7a08d04SJames Feist                                                "match ";
398b7a08d04SJames Feist                                         messages::internalError(asyncResp->res);
399b7a08d04SJames Feist                                         return;
400b7a08d04SJames Feist                                     }
401b7a08d04SJames Feist                                     nlohmann::json& steps = (*config)["Steps"];
402b7a08d04SJames Feist                                     steps = nlohmann::json::array();
403b7a08d04SJames Feist                                     for (size_t ii = 0; ii < keys->size(); ii++)
404b7a08d04SJames Feist                                     {
405b7a08d04SJames Feist                                         steps.push_back(
406b7a08d04SJames Feist                                             {{"Target", (*keys)[ii]},
407b7a08d04SJames Feist                                              {"Output", (*values)[ii]}});
408b7a08d04SJames Feist                                     }
409b7a08d04SJames Feist                                 }
410b7a08d04SJames Feist                             }
411b7a08d04SJames Feist                             if (propertyPair.first == "NegativeHysteresis" ||
412b7a08d04SJames Feist                                 propertyPair.first == "PositiveHysteresis")
413b7a08d04SJames Feist                             {
414b7a08d04SJames Feist                                 const double* ptr =
415abf2add6SEd Tanous                                     std::get_if<double>(&propertyPair.second);
416b7a08d04SJames Feist                                 if (ptr == nullptr)
417b7a08d04SJames Feist                                 {
418b7a08d04SJames Feist                                     BMCWEB_LOG_ERROR << "Field Illegal "
419b7a08d04SJames Feist                                                      << propertyPair.first;
420b7a08d04SJames Feist                                     messages::internalError(asyncResp->res);
421b7a08d04SJames Feist                                     return;
422b7a08d04SJames Feist                                 }
423b7a08d04SJames Feist                                 (*config)[propertyPair.first] = *ptr;
424b7a08d04SJames Feist                             }
425b7a08d04SJames Feist                         }
426b7a08d04SJames Feist 
427b7a08d04SJames Feist                         // pid and fans are off the same configuration
428b7a08d04SJames Feist                         if (intfPair.first == pidConfigurationIface ||
429b7a08d04SJames Feist                             intfPair.first == stepwiseConfigurationIface)
430b7a08d04SJames Feist                         {
4315b4aa86bSJames Feist 
4325b4aa86bSJames Feist                             if (propertyPair.first == "Zones")
4335b4aa86bSJames Feist                             {
4345b4aa86bSJames Feist                                 const std::vector<std::string>* inputs =
435abf2add6SEd Tanous                                     std::get_if<std::vector<std::string>>(
4361b6b96c5SEd Tanous                                         &propertyPair.second);
4375b4aa86bSJames Feist 
4385b4aa86bSJames Feist                                 if (inputs == nullptr)
4395b4aa86bSJames Feist                                 {
4405b4aa86bSJames Feist                                     BMCWEB_LOG_ERROR
4415b4aa86bSJames Feist                                         << "Zones Pid Field Illegal";
442a08b46ccSJason M. Bills                                     messages::internalError(asyncResp->res);
4435b4aa86bSJames Feist                                     return;
4445b4aa86bSJames Feist                                 }
445b7a08d04SJames Feist                                 auto& data = (*config)[propertyPair.first];
4465b4aa86bSJames Feist                                 data = nlohmann::json::array();
4475b4aa86bSJames Feist                                 for (std::string itemCopy : *inputs)
4485b4aa86bSJames Feist                                 {
4495b4aa86bSJames Feist                                     dbus::utility::escapePathForDbus(itemCopy);
4505b4aa86bSJames Feist                                     data.push_back(
4515b4aa86bSJames Feist                                         {{"@odata.id",
4525b4aa86bSJames Feist                                           "/redfish/v1/Managers/bmc#/Oem/"
4535b4aa86bSJames Feist                                           "OpenBmc/Fan/FanZones/" +
4545b4aa86bSJames Feist                                               itemCopy}});
4555b4aa86bSJames Feist                                 }
4565b4aa86bSJames Feist                             }
4575b4aa86bSJames Feist                             // todo(james): may never happen, but this
4585b4aa86bSJames Feist                             // assumes configuration data referenced in the
4595b4aa86bSJames Feist                             // PID config is provided by the same daemon, we
4605b4aa86bSJames Feist                             // could add another loop to cover all cases,
4615b4aa86bSJames Feist                             // but I'm okay kicking this can down the road a
4625b4aa86bSJames Feist                             // bit
4635b4aa86bSJames Feist 
4645b4aa86bSJames Feist                             else if (propertyPair.first == "Inputs" ||
4655b4aa86bSJames Feist                                      propertyPair.first == "Outputs")
4665b4aa86bSJames Feist                             {
467b7a08d04SJames Feist                                 auto& data = (*config)[propertyPair.first];
4685b4aa86bSJames Feist                                 const std::vector<std::string>* inputs =
469abf2add6SEd Tanous                                     std::get_if<std::vector<std::string>>(
4701b6b96c5SEd Tanous                                         &propertyPair.second);
4715b4aa86bSJames Feist 
4725b4aa86bSJames Feist                                 if (inputs == nullptr)
4735b4aa86bSJames Feist                                 {
4745b4aa86bSJames Feist                                     BMCWEB_LOG_ERROR << "Field Illegal "
4755b4aa86bSJames Feist                                                      << propertyPair.first;
476f12894f8SJason M. Bills                                     messages::internalError(asyncResp->res);
4775b4aa86bSJames Feist                                     return;
4785b4aa86bSJames Feist                                 }
4795b4aa86bSJames Feist                                 data = *inputs;
480b943aaefSJames Feist                             }
481b943aaefSJames Feist                             else if (propertyPair.first == "SetPointOffset")
482b943aaefSJames Feist                             {
483b943aaefSJames Feist                                 const std::string* ptr =
484b943aaefSJames Feist                                     std::get_if<std::string>(
485b943aaefSJames Feist                                         &propertyPair.second);
486b943aaefSJames Feist 
487b943aaefSJames Feist                                 if (ptr == nullptr)
488b943aaefSJames Feist                                 {
489b943aaefSJames Feist                                     BMCWEB_LOG_ERROR << "Field Illegal "
490b943aaefSJames Feist                                                      << propertyPair.first;
491b943aaefSJames Feist                                     messages::internalError(asyncResp->res);
492b943aaefSJames Feist                                     return;
493b943aaefSJames Feist                                 }
494b943aaefSJames Feist                                 // translate from dbus to redfish
495b943aaefSJames Feist                                 if (*ptr == "WarningHigh")
496b943aaefSJames Feist                                 {
497b943aaefSJames Feist                                     (*config)["SetPointOffset"] =
498b943aaefSJames Feist                                         "UpperThresholdNonCritical";
499b943aaefSJames Feist                                 }
500b943aaefSJames Feist                                 else if (*ptr == "WarningLow")
501b943aaefSJames Feist                                 {
502b943aaefSJames Feist                                     (*config)["SetPointOffset"] =
503b943aaefSJames Feist                                         "LowerThresholdNonCritical";
504b943aaefSJames Feist                                 }
505b943aaefSJames Feist                                 else if (*ptr == "CriticalHigh")
506b943aaefSJames Feist                                 {
507b943aaefSJames Feist                                     (*config)["SetPointOffset"] =
508b943aaefSJames Feist                                         "UpperThresholdCritical";
509b943aaefSJames Feist                                 }
510b943aaefSJames Feist                                 else if (*ptr == "CriticalLow")
511b943aaefSJames Feist                                 {
512b943aaefSJames Feist                                     (*config)["SetPointOffset"] =
513b943aaefSJames Feist                                         "LowerThresholdCritical";
514b943aaefSJames Feist                                 }
515b943aaefSJames Feist                                 else
516b943aaefSJames Feist                                 {
517b943aaefSJames Feist                                     BMCWEB_LOG_ERROR << "Value Illegal "
518b943aaefSJames Feist                                                      << *ptr;
519b943aaefSJames Feist                                     messages::internalError(asyncResp->res);
520b943aaefSJames Feist                                     return;
521b943aaefSJames Feist                                 }
522b943aaefSJames Feist                             }
523b943aaefSJames Feist                             // doubles
5245b4aa86bSJames Feist                             else if (propertyPair.first ==
5255b4aa86bSJames Feist                                          "FFGainCoefficient" ||
5265b4aa86bSJames Feist                                      propertyPair.first == "FFOffCoefficient" ||
5275b4aa86bSJames Feist                                      propertyPair.first == "ICoefficient" ||
5285b4aa86bSJames Feist                                      propertyPair.first == "ILimitMax" ||
5295b4aa86bSJames Feist                                      propertyPair.first == "ILimitMin" ||
530aad1a257SJames Feist                                      propertyPair.first ==
531aad1a257SJames Feist                                          "PositiveHysteresis" ||
532aad1a257SJames Feist                                      propertyPair.first ==
533aad1a257SJames Feist                                          "NegativeHysteresis" ||
5345b4aa86bSJames Feist                                      propertyPair.first == "OutLimitMax" ||
5355b4aa86bSJames Feist                                      propertyPair.first == "OutLimitMin" ||
5365b4aa86bSJames Feist                                      propertyPair.first == "PCoefficient" ||
5377625cb81SJames Feist                                      propertyPair.first == "SetPoint" ||
5385b4aa86bSJames Feist                                      propertyPair.first == "SlewNeg" ||
5395b4aa86bSJames Feist                                      propertyPair.first == "SlewPos")
5405b4aa86bSJames Feist                             {
5415b4aa86bSJames Feist                                 const double* ptr =
542abf2add6SEd Tanous                                     std::get_if<double>(&propertyPair.second);
5435b4aa86bSJames Feist                                 if (ptr == nullptr)
5445b4aa86bSJames Feist                                 {
5455b4aa86bSJames Feist                                     BMCWEB_LOG_ERROR << "Field Illegal "
5465b4aa86bSJames Feist                                                      << propertyPair.first;
547f12894f8SJason M. Bills                                     messages::internalError(asyncResp->res);
5485b4aa86bSJames Feist                                     return;
5495b4aa86bSJames Feist                                 }
550b7a08d04SJames Feist                                 (*config)[propertyPair.first] = *ptr;
5515b4aa86bSJames Feist                             }
5525b4aa86bSJames Feist                         }
5535b4aa86bSJames Feist                     }
5545b4aa86bSJames Feist                 }
5555b4aa86bSJames Feist             }
5565b4aa86bSJames Feist         },
5575b4aa86bSJames Feist         connection, path, objectManagerIface, "GetManagedObjects");
5585b4aa86bSJames Feist }
559ca537928SJennifer Lee 
56083ff9ab6SJames Feist enum class CreatePIDRet
56183ff9ab6SJames Feist {
56283ff9ab6SJames Feist     fail,
56383ff9ab6SJames Feist     del,
56483ff9ab6SJames Feist     patch
56583ff9ab6SJames Feist };
56683ff9ab6SJames Feist 
5675f2caaefSJames Feist static bool getZonesFromJsonReq(const std::shared_ptr<AsyncResp>& response,
5685f2caaefSJames Feist                                 std::vector<nlohmann::json>& config,
5695f2caaefSJames Feist                                 std::vector<std::string>& zones)
5705f2caaefSJames Feist {
571b6baeaa4SJames Feist     if (config.empty())
572b6baeaa4SJames Feist     {
573b6baeaa4SJames Feist         BMCWEB_LOG_ERROR << "Empty Zones";
574b6baeaa4SJames Feist         messages::propertyValueFormatError(response->res,
575b6baeaa4SJames Feist                                            nlohmann::json::array(), "Zones");
576b6baeaa4SJames Feist         return false;
577b6baeaa4SJames Feist     }
5785f2caaefSJames Feist     for (auto& odata : config)
5795f2caaefSJames Feist     {
5805f2caaefSJames Feist         std::string path;
5815f2caaefSJames Feist         if (!redfish::json_util::readJson(odata, response->res, "@odata.id",
5825f2caaefSJames Feist                                           path))
5835f2caaefSJames Feist         {
5845f2caaefSJames Feist             return false;
5855f2caaefSJames Feist         }
5865f2caaefSJames Feist         std::string input;
58761adbda3SJames Feist 
58861adbda3SJames Feist         // 8 below comes from
58961adbda3SJames Feist         // /redfish/v1/Managers/bmc#/Oem/OpenBmc/Fan/FanZones/Left
59061adbda3SJames Feist         //     0    1     2      3    4    5      6     7      8
59161adbda3SJames Feist         if (!dbus::utility::getNthStringFromPath(path, 8, input))
5925f2caaefSJames Feist         {
5935f2caaefSJames Feist             BMCWEB_LOG_ERROR << "Got invalid path " << path;
5945f2caaefSJames Feist             BMCWEB_LOG_ERROR << "Illegal Type Zones";
5955f2caaefSJames Feist             messages::propertyValueFormatError(response->res, odata.dump(),
5965f2caaefSJames Feist                                                "Zones");
5975f2caaefSJames Feist             return false;
5985f2caaefSJames Feist         }
5995f2caaefSJames Feist         boost::replace_all(input, "_", " ");
6005f2caaefSJames Feist         zones.emplace_back(std::move(input));
6015f2caaefSJames Feist     }
6025f2caaefSJames Feist     return true;
6035f2caaefSJames Feist }
6045f2caaefSJames Feist 
60573df0db0SJames Feist static const dbus::utility::ManagedItem*
60673df0db0SJames Feist     findChassis(const dbus::utility::ManagedObjectType& managedObj,
607b6baeaa4SJames Feist                 const std::string& value, std::string& chassis)
608b6baeaa4SJames Feist {
609b6baeaa4SJames Feist     BMCWEB_LOG_DEBUG << "Find Chassis: " << value << "\n";
610b6baeaa4SJames Feist 
611b6baeaa4SJames Feist     std::string escaped = boost::replace_all_copy(value, " ", "_");
612b6baeaa4SJames Feist     escaped = "/" + escaped;
613b6baeaa4SJames Feist     auto it = std::find_if(
614b6baeaa4SJames Feist         managedObj.begin(), managedObj.end(), [&escaped](const auto& obj) {
615b6baeaa4SJames Feist             if (boost::algorithm::ends_with(obj.first.str, escaped))
616b6baeaa4SJames Feist             {
617b6baeaa4SJames Feist                 BMCWEB_LOG_DEBUG << "Matched " << obj.first.str << "\n";
618b6baeaa4SJames Feist                 return true;
619b6baeaa4SJames Feist             }
620b6baeaa4SJames Feist             return false;
621b6baeaa4SJames Feist         });
622b6baeaa4SJames Feist 
623b6baeaa4SJames Feist     if (it == managedObj.end())
624b6baeaa4SJames Feist     {
62573df0db0SJames Feist         return nullptr;
626b6baeaa4SJames Feist     }
627b6baeaa4SJames Feist     // 5 comes from <chassis-name> being the 5th element
628b6baeaa4SJames Feist     // /xyz/openbmc_project/inventory/system/chassis/<chassis-name>
62973df0db0SJames Feist     if (dbus::utility::getNthStringFromPath(it->first.str, 5, chassis))
63073df0db0SJames Feist     {
63173df0db0SJames Feist         return &(*it);
63273df0db0SJames Feist     }
63373df0db0SJames Feist 
63473df0db0SJames Feist     return nullptr;
635b6baeaa4SJames Feist }
636b6baeaa4SJames Feist 
63783ff9ab6SJames Feist static CreatePIDRet createPidInterface(
63883ff9ab6SJames Feist     const std::shared_ptr<AsyncResp>& response, const std::string& type,
639b6baeaa4SJames Feist     nlohmann::json::iterator it, const std::string& path,
64083ff9ab6SJames Feist     const dbus::utility::ManagedObjectType& managedObj, bool createNewObject,
64183ff9ab6SJames Feist     boost::container::flat_map<std::string, dbus::utility::DbusVariantType>&
64283ff9ab6SJames Feist         output,
64373df0db0SJames Feist     std::string& chassis, const std::string& profile)
64483ff9ab6SJames Feist {
64583ff9ab6SJames Feist 
6465f2caaefSJames Feist     // common deleter
647b6baeaa4SJames Feist     if (it.value() == nullptr)
6485f2caaefSJames Feist     {
6495f2caaefSJames Feist         std::string iface;
6505f2caaefSJames Feist         if (type == "PidControllers" || type == "FanControllers")
6515f2caaefSJames Feist         {
6525f2caaefSJames Feist             iface = pidConfigurationIface;
6535f2caaefSJames Feist         }
6545f2caaefSJames Feist         else if (type == "FanZones")
6555f2caaefSJames Feist         {
6565f2caaefSJames Feist             iface = pidZoneConfigurationIface;
6575f2caaefSJames Feist         }
6585f2caaefSJames Feist         else if (type == "StepwiseControllers")
6595f2caaefSJames Feist         {
6605f2caaefSJames Feist             iface = stepwiseConfigurationIface;
6615f2caaefSJames Feist         }
6625f2caaefSJames Feist         else
6635f2caaefSJames Feist         {
6645f2caaefSJames Feist             BMCWEB_LOG_ERROR << "Line:" << __LINE__ << ", Illegal Type "
6655f2caaefSJames Feist                              << type;
6665f2caaefSJames Feist             messages::propertyUnknown(response->res, type);
6675f2caaefSJames Feist             return CreatePIDRet::fail;
6685f2caaefSJames Feist         }
669*6ee7f774SJames Feist 
670*6ee7f774SJames Feist         BMCWEB_LOG_DEBUG << "del " << path << " " << iface << "\n";
6715f2caaefSJames Feist         // delete interface
6725f2caaefSJames Feist         crow::connections::systemBus->async_method_call(
6735f2caaefSJames Feist             [response, path](const boost::system::error_code ec) {
6745f2caaefSJames Feist                 if (ec)
6755f2caaefSJames Feist                 {
6765f2caaefSJames Feist                     BMCWEB_LOG_ERROR << "Error patching " << path << ": " << ec;
6775f2caaefSJames Feist                     messages::internalError(response->res);
678b6baeaa4SJames Feist                     return;
6795f2caaefSJames Feist                 }
680b6baeaa4SJames Feist                 messages::success(response->res);
6815f2caaefSJames Feist             },
6825f2caaefSJames Feist             "xyz.openbmc_project.EntityManager", path, iface, "Delete");
6835f2caaefSJames Feist         return CreatePIDRet::del;
6845f2caaefSJames Feist     }
6855f2caaefSJames Feist 
68673df0db0SJames Feist     const dbus::utility::ManagedItem* managedItem = nullptr;
687b6baeaa4SJames Feist     if (!createNewObject)
688b6baeaa4SJames Feist     {
689b6baeaa4SJames Feist         // if we aren't creating a new object, we should be able to find it on
690b6baeaa4SJames Feist         // d-bus
69173df0db0SJames Feist         managedItem = findChassis(managedObj, it.key(), chassis);
69273df0db0SJames Feist         if (managedItem == nullptr)
693b6baeaa4SJames Feist         {
694b6baeaa4SJames Feist             BMCWEB_LOG_ERROR << "Failed to get chassis from config patch";
695b6baeaa4SJames Feist             messages::invalidObject(response->res, it.key());
696b6baeaa4SJames Feist             return CreatePIDRet::fail;
697b6baeaa4SJames Feist         }
698b6baeaa4SJames Feist     }
699b6baeaa4SJames Feist 
70073df0db0SJames Feist     if (profile.size() &&
70173df0db0SJames Feist         (type == "PidControllers" || type == "FanControllers" ||
70273df0db0SJames Feist          type == "StepwiseControllers"))
70373df0db0SJames Feist     {
70473df0db0SJames Feist         if (managedItem == nullptr)
70573df0db0SJames Feist         {
70673df0db0SJames Feist             output["Profiles"] = std::vector<std::string>{profile};
70773df0db0SJames Feist         }
70873df0db0SJames Feist         else
70973df0db0SJames Feist         {
71073df0db0SJames Feist             std::string interface;
71173df0db0SJames Feist             if (type == "StepwiseControllers")
71273df0db0SJames Feist             {
71373df0db0SJames Feist                 interface = stepwiseConfigurationIface;
71473df0db0SJames Feist             }
71573df0db0SJames Feist             else
71673df0db0SJames Feist             {
71773df0db0SJames Feist                 interface = pidConfigurationIface;
71873df0db0SJames Feist             }
71973df0db0SJames Feist             auto findConfig = managedItem->second.find(interface);
72073df0db0SJames Feist             if (findConfig == managedItem->second.end())
72173df0db0SJames Feist             {
72273df0db0SJames Feist                 BMCWEB_LOG_ERROR
72373df0db0SJames Feist                     << "Failed to find interface in managed object";
72473df0db0SJames Feist                 messages::internalError(response->res);
72573df0db0SJames Feist                 return CreatePIDRet::fail;
72673df0db0SJames Feist             }
72773df0db0SJames Feist             auto findProfiles = findConfig->second.find("Profiles");
72873df0db0SJames Feist             if (findProfiles != findConfig->second.end())
72973df0db0SJames Feist             {
73073df0db0SJames Feist                 const std::vector<std::string>* curProfiles =
73173df0db0SJames Feist                     std::get_if<std::vector<std::string>>(
73273df0db0SJames Feist                         &(findProfiles->second));
73373df0db0SJames Feist                 if (curProfiles == nullptr)
73473df0db0SJames Feist                 {
73573df0db0SJames Feist                     BMCWEB_LOG_ERROR << "Illegal profiles in managed object";
73673df0db0SJames Feist                     messages::internalError(response->res);
73773df0db0SJames Feist                     return CreatePIDRet::fail;
73873df0db0SJames Feist                 }
73973df0db0SJames Feist                 if (std::find(curProfiles->begin(), curProfiles->end(),
74073df0db0SJames Feist                               profile) == curProfiles->end())
74173df0db0SJames Feist                 {
74273df0db0SJames Feist                     std::vector<std::string> newProfiles = *curProfiles;
74373df0db0SJames Feist                     newProfiles.push_back(profile);
74473df0db0SJames Feist                     output["Profiles"] = newProfiles;
74573df0db0SJames Feist                 }
74673df0db0SJames Feist             }
74773df0db0SJames Feist         }
74873df0db0SJames Feist     }
74973df0db0SJames Feist 
75083ff9ab6SJames Feist     if (type == "PidControllers" || type == "FanControllers")
75183ff9ab6SJames Feist     {
75283ff9ab6SJames Feist         if (createNewObject)
75383ff9ab6SJames Feist         {
75483ff9ab6SJames Feist             output["Class"] = type == "PidControllers" ? std::string("temp")
75583ff9ab6SJames Feist                                                        : std::string("fan");
75683ff9ab6SJames Feist             output["Type"] = std::string("Pid");
75783ff9ab6SJames Feist         }
7585f2caaefSJames Feist 
7595f2caaefSJames Feist         std::optional<std::vector<nlohmann::json>> zones;
7605f2caaefSJames Feist         std::optional<std::vector<std::string>> inputs;
7615f2caaefSJames Feist         std::optional<std::vector<std::string>> outputs;
7625f2caaefSJames Feist         std::map<std::string, std::optional<double>> doubles;
763b943aaefSJames Feist         std::optional<std::string> setpointOffset;
7645f2caaefSJames Feist         if (!redfish::json_util::readJson(
765b6baeaa4SJames Feist                 it.value(), response->res, "Inputs", inputs, "Outputs", outputs,
7665f2caaefSJames Feist                 "Zones", zones, "FFGainCoefficient",
7675f2caaefSJames Feist                 doubles["FFGainCoefficient"], "FFOffCoefficient",
7685f2caaefSJames Feist                 doubles["FFOffCoefficient"], "ICoefficient",
7695f2caaefSJames Feist                 doubles["ICoefficient"], "ILimitMax", doubles["ILimitMax"],
7705f2caaefSJames Feist                 "ILimitMin", doubles["ILimitMin"], "OutLimitMax",
7715f2caaefSJames Feist                 doubles["OutLimitMax"], "OutLimitMin", doubles["OutLimitMin"],
7725f2caaefSJames Feist                 "PCoefficient", doubles["PCoefficient"], "SetPoint",
773b943aaefSJames Feist                 doubles["SetPoint"], "SetPointOffset", setpointOffset,
774b943aaefSJames Feist                 "SlewNeg", doubles["SlewNeg"], "SlewPos", doubles["SlewPos"],
775b943aaefSJames Feist                 "PositiveHysteresis", doubles["PositiveHysteresis"],
776b943aaefSJames Feist                 "NegativeHysteresis", doubles["NegativeHysteresis"]))
77783ff9ab6SJames Feist         {
7785f2caaefSJames Feist             BMCWEB_LOG_ERROR << "Line:" << __LINE__ << ", Illegal Property "
779b6baeaa4SJames Feist                              << it.value().dump();
7805f2caaefSJames Feist             return CreatePIDRet::fail;
78183ff9ab6SJames Feist         }
7825f2caaefSJames Feist         if (zones)
7835f2caaefSJames Feist         {
7845f2caaefSJames Feist             std::vector<std::string> zonesStr;
7855f2caaefSJames Feist             if (!getZonesFromJsonReq(response, *zones, zonesStr))
7865f2caaefSJames Feist             {
7875f2caaefSJames Feist                 BMCWEB_LOG_ERROR << "Line:" << __LINE__ << ", Illegal Zones";
7885f2caaefSJames Feist                 return CreatePIDRet::fail;
7895f2caaefSJames Feist             }
790b6baeaa4SJames Feist             if (chassis.empty() &&
791b6baeaa4SJames Feist                 !findChassis(managedObj, zonesStr[0], chassis))
792b6baeaa4SJames Feist             {
793b6baeaa4SJames Feist                 BMCWEB_LOG_ERROR << "Failed to get chassis from config patch";
794b6baeaa4SJames Feist                 messages::invalidObject(response->res, it.key());
795b6baeaa4SJames Feist                 return CreatePIDRet::fail;
796b6baeaa4SJames Feist             }
797b6baeaa4SJames Feist 
7985f2caaefSJames Feist             output["Zones"] = std::move(zonesStr);
7995f2caaefSJames Feist         }
8005f2caaefSJames Feist         if (inputs || outputs)
8015f2caaefSJames Feist         {
8025f2caaefSJames Feist             std::array<std::optional<std::vector<std::string>>*, 2> containers =
8035f2caaefSJames Feist                 {&inputs, &outputs};
8045f2caaefSJames Feist             size_t index = 0;
8055f2caaefSJames Feist             for (const auto& containerPtr : containers)
8065f2caaefSJames Feist             {
8075f2caaefSJames Feist                 std::optional<std::vector<std::string>>& container =
8085f2caaefSJames Feist                     *containerPtr;
8095f2caaefSJames Feist                 if (!container)
8105f2caaefSJames Feist                 {
8115f2caaefSJames Feist                     index++;
8125f2caaefSJames Feist                     continue;
81383ff9ab6SJames Feist                 }
81483ff9ab6SJames Feist 
8155f2caaefSJames Feist                 for (std::string& value : *container)
81683ff9ab6SJames Feist                 {
8175f2caaefSJames Feist                     boost::replace_all(value, "_", " ");
81883ff9ab6SJames Feist                 }
8195f2caaefSJames Feist                 std::string key;
8205f2caaefSJames Feist                 if (index == 0)
8215f2caaefSJames Feist                 {
8225f2caaefSJames Feist                     key = "Inputs";
8235f2caaefSJames Feist                 }
8245f2caaefSJames Feist                 else
8255f2caaefSJames Feist                 {
8265f2caaefSJames Feist                     key = "Outputs";
8275f2caaefSJames Feist                 }
8285f2caaefSJames Feist                 output[key] = *container;
8295f2caaefSJames Feist                 index++;
8305f2caaefSJames Feist             }
83183ff9ab6SJames Feist         }
83283ff9ab6SJames Feist 
833b943aaefSJames Feist         if (setpointOffset)
834b943aaefSJames Feist         {
835b943aaefSJames Feist             // translate between redfish and dbus names
836b943aaefSJames Feist             if (*setpointOffset == "UpperThresholdNonCritical")
837b943aaefSJames Feist             {
838b943aaefSJames Feist                 output["SetPointOffset"] = std::string("WarningLow");
839b943aaefSJames Feist             }
840b943aaefSJames Feist             else if (*setpointOffset == "LowerThresholdNonCritical")
841b943aaefSJames Feist             {
842b943aaefSJames Feist                 output["SetPointOffset"] = std::string("WarningHigh");
843b943aaefSJames Feist             }
844b943aaefSJames Feist             else if (*setpointOffset == "LowerThresholdCritical")
845b943aaefSJames Feist             {
846b943aaefSJames Feist                 output["SetPointOffset"] = std::string("CriticalLow");
847b943aaefSJames Feist             }
848b943aaefSJames Feist             else if (*setpointOffset == "UpperThresholdCritical")
849b943aaefSJames Feist             {
850b943aaefSJames Feist                 output["SetPointOffset"] = std::string("CriticalHigh");
851b943aaefSJames Feist             }
852b943aaefSJames Feist             else
853b943aaefSJames Feist             {
854b943aaefSJames Feist                 BMCWEB_LOG_ERROR << "Invalid setpointoffset "
855b943aaefSJames Feist                                  << *setpointOffset;
856b943aaefSJames Feist                 messages::invalidObject(response->res, it.key());
857b943aaefSJames Feist                 return CreatePIDRet::fail;
858b943aaefSJames Feist             }
859b943aaefSJames Feist         }
860b943aaefSJames Feist 
86183ff9ab6SJames Feist         // doubles
8625f2caaefSJames Feist         for (const auto& pairs : doubles)
86383ff9ab6SJames Feist         {
8645f2caaefSJames Feist             if (!pairs.second)
86583ff9ab6SJames Feist             {
8665f2caaefSJames Feist                 continue;
86783ff9ab6SJames Feist             }
8685f2caaefSJames Feist             BMCWEB_LOG_DEBUG << pairs.first << " = " << *pairs.second;
8695f2caaefSJames Feist             output[pairs.first] = *(pairs.second);
8705f2caaefSJames Feist         }
87183ff9ab6SJames Feist     }
87283ff9ab6SJames Feist 
87383ff9ab6SJames Feist     else if (type == "FanZones")
87483ff9ab6SJames Feist     {
87583ff9ab6SJames Feist         output["Type"] = std::string("Pid.Zone");
87683ff9ab6SJames Feist 
8775f2caaefSJames Feist         std::optional<nlohmann::json> chassisContainer;
8785f2caaefSJames Feist         std::optional<double> failSafePercent;
879d3ec07f8SJames Feist         std::optional<double> minThermalOutput;
880b6baeaa4SJames Feist         if (!redfish::json_util::readJson(it.value(), response->res, "Chassis",
8815f2caaefSJames Feist                                           chassisContainer, "FailSafePercent",
882d3ec07f8SJames Feist                                           failSafePercent, "MinThermalOutput",
883d3ec07f8SJames Feist                                           minThermalOutput))
88483ff9ab6SJames Feist         {
8855f2caaefSJames Feist             BMCWEB_LOG_ERROR << "Line:" << __LINE__ << ", Illegal Property "
886b6baeaa4SJames Feist                              << it.value().dump();
88783ff9ab6SJames Feist             return CreatePIDRet::fail;
88883ff9ab6SJames Feist         }
8895f2caaefSJames Feist 
8905f2caaefSJames Feist         if (chassisContainer)
89183ff9ab6SJames Feist         {
8925f2caaefSJames Feist 
8935f2caaefSJames Feist             std::string chassisId;
8945f2caaefSJames Feist             if (!redfish::json_util::readJson(*chassisContainer, response->res,
8955f2caaefSJames Feist                                               "@odata.id", chassisId))
8965f2caaefSJames Feist             {
8975f2caaefSJames Feist                 BMCWEB_LOG_ERROR << "Line:" << __LINE__ << ", Illegal Property "
8985f2caaefSJames Feist                                  << chassisContainer->dump();
89983ff9ab6SJames Feist                 return CreatePIDRet::fail;
90083ff9ab6SJames Feist             }
90183ff9ab6SJames Feist 
902717794d5SAppaRao Puli             // /redfish/v1/chassis/chassis_name/
9035f2caaefSJames Feist             if (!dbus::utility::getNthStringFromPath(chassisId, 3, chassis))
90483ff9ab6SJames Feist             {
9055f2caaefSJames Feist                 BMCWEB_LOG_ERROR << "Got invalid path " << chassisId;
9065f2caaefSJames Feist                 messages::invalidObject(response->res, chassisId);
90783ff9ab6SJames Feist                 return CreatePIDRet::fail;
90883ff9ab6SJames Feist             }
90983ff9ab6SJames Feist         }
910d3ec07f8SJames Feist         if (minThermalOutput)
91183ff9ab6SJames Feist         {
912d3ec07f8SJames Feist             output["MinThermalOutput"] = *minThermalOutput;
9135f2caaefSJames Feist         }
9145f2caaefSJames Feist         if (failSafePercent)
91583ff9ab6SJames Feist         {
9165f2caaefSJames Feist             output["FailSafePercent"] = *failSafePercent;
9175f2caaefSJames Feist         }
9185f2caaefSJames Feist     }
9195f2caaefSJames Feist     else if (type == "StepwiseControllers")
9205f2caaefSJames Feist     {
9215f2caaefSJames Feist         output["Type"] = std::string("Stepwise");
9225f2caaefSJames Feist 
9235f2caaefSJames Feist         std::optional<std::vector<nlohmann::json>> zones;
9245f2caaefSJames Feist         std::optional<std::vector<nlohmann::json>> steps;
9255f2caaefSJames Feist         std::optional<std::vector<std::string>> inputs;
9265f2caaefSJames Feist         std::optional<double> positiveHysteresis;
9275f2caaefSJames Feist         std::optional<double> negativeHysteresis;
928c33a90ecSJames Feist         std::optional<std::string> direction; // upper clipping curve vs lower
9295f2caaefSJames Feist         if (!redfish::json_util::readJson(
930b6baeaa4SJames Feist                 it.value(), response->res, "Zones", zones, "Steps", steps,
931b6baeaa4SJames Feist                 "Inputs", inputs, "PositiveHysteresis", positiveHysteresis,
932c33a90ecSJames Feist                 "NegativeHysteresis", negativeHysteresis, "Direction",
933c33a90ecSJames Feist                 direction))
9345f2caaefSJames Feist         {
9355f2caaefSJames Feist             BMCWEB_LOG_ERROR << "Line:" << __LINE__ << ", Illegal Property "
936b6baeaa4SJames Feist                              << it.value().dump();
93783ff9ab6SJames Feist             return CreatePIDRet::fail;
93883ff9ab6SJames Feist         }
9395f2caaefSJames Feist 
9405f2caaefSJames Feist         if (zones)
94183ff9ab6SJames Feist         {
942b6baeaa4SJames Feist             std::vector<std::string> zonesStrs;
943b6baeaa4SJames Feist             if (!getZonesFromJsonReq(response, *zones, zonesStrs))
9445f2caaefSJames Feist             {
9455f2caaefSJames Feist                 BMCWEB_LOG_ERROR << "Line:" << __LINE__ << ", Illegal Zones";
94683ff9ab6SJames Feist                 return CreatePIDRet::fail;
94783ff9ab6SJames Feist             }
948b6baeaa4SJames Feist             if (chassis.empty() &&
949b6baeaa4SJames Feist                 !findChassis(managedObj, zonesStrs[0], chassis))
950b6baeaa4SJames Feist             {
951b6baeaa4SJames Feist                 BMCWEB_LOG_ERROR << "Failed to get chassis from config patch";
952b6baeaa4SJames Feist                 messages::invalidObject(response->res, it.key());
953b6baeaa4SJames Feist                 return CreatePIDRet::fail;
954b6baeaa4SJames Feist             }
955b6baeaa4SJames Feist             output["Zones"] = std::move(zonesStrs);
9565f2caaefSJames Feist         }
9575f2caaefSJames Feist         if (steps)
9585f2caaefSJames Feist         {
9595f2caaefSJames Feist             std::vector<double> readings;
9605f2caaefSJames Feist             std::vector<double> outputs;
9615f2caaefSJames Feist             for (auto& step : *steps)
9625f2caaefSJames Feist             {
9635f2caaefSJames Feist                 double target;
964b01bf299SEd Tanous                 double output;
9655f2caaefSJames Feist 
9665f2caaefSJames Feist                 if (!redfish::json_util::readJson(step, response->res, "Target",
967b01bf299SEd Tanous                                                   target, "Output", output))
9685f2caaefSJames Feist                 {
9695f2caaefSJames Feist                     BMCWEB_LOG_ERROR << "Line:" << __LINE__
970b6baeaa4SJames Feist                                      << ", Illegal Property "
971b6baeaa4SJames Feist                                      << it.value().dump();
9725f2caaefSJames Feist                     return CreatePIDRet::fail;
9735f2caaefSJames Feist                 }
9745f2caaefSJames Feist                 readings.emplace_back(target);
975b01bf299SEd Tanous                 outputs.emplace_back(output);
9765f2caaefSJames Feist             }
9775f2caaefSJames Feist             output["Reading"] = std::move(readings);
9785f2caaefSJames Feist             output["Output"] = std::move(outputs);
9795f2caaefSJames Feist         }
9805f2caaefSJames Feist         if (inputs)
9815f2caaefSJames Feist         {
9825f2caaefSJames Feist             for (std::string& value : *inputs)
9835f2caaefSJames Feist             {
9845f2caaefSJames Feist                 boost::replace_all(value, "_", " ");
9855f2caaefSJames Feist             }
9865f2caaefSJames Feist             output["Inputs"] = std::move(*inputs);
9875f2caaefSJames Feist         }
9885f2caaefSJames Feist         if (negativeHysteresis)
9895f2caaefSJames Feist         {
9905f2caaefSJames Feist             output["NegativeHysteresis"] = *negativeHysteresis;
9915f2caaefSJames Feist         }
9925f2caaefSJames Feist         if (positiveHysteresis)
9935f2caaefSJames Feist         {
9945f2caaefSJames Feist             output["PositiveHysteresis"] = *positiveHysteresis;
99583ff9ab6SJames Feist         }
996c33a90ecSJames Feist         if (direction)
997c33a90ecSJames Feist         {
998c33a90ecSJames Feist             constexpr const std::array<const char*, 2> allowedDirections = {
999c33a90ecSJames Feist                 "Ceiling", "Floor"};
1000c33a90ecSJames Feist             if (std::find(allowedDirections.begin(), allowedDirections.end(),
1001c33a90ecSJames Feist                           *direction) == allowedDirections.end())
1002c33a90ecSJames Feist             {
1003c33a90ecSJames Feist                 messages::propertyValueTypeError(response->res, "Direction",
1004c33a90ecSJames Feist                                                  *direction);
1005c33a90ecSJames Feist                 return CreatePIDRet::fail;
1006c33a90ecSJames Feist             }
1007c33a90ecSJames Feist             output["Class"] = *direction;
1008c33a90ecSJames Feist         }
100983ff9ab6SJames Feist     }
101083ff9ab6SJames Feist     else
101183ff9ab6SJames Feist     {
10125f2caaefSJames Feist         BMCWEB_LOG_ERROR << "Line:" << __LINE__ << ", Illegal Type " << type;
101335a62c7cSJason M. Bills         messages::propertyUnknown(response->res, type);
101483ff9ab6SJames Feist         return CreatePIDRet::fail;
101583ff9ab6SJames Feist     }
101683ff9ab6SJames Feist     return CreatePIDRet::patch;
101783ff9ab6SJames Feist }
101873df0db0SJames Feist struct GetPIDValues : std::enable_shared_from_this<GetPIDValues>
101973df0db0SJames Feist {
102083ff9ab6SJames Feist 
102173df0db0SJames Feist     GetPIDValues(const std::shared_ptr<AsyncResp>& asyncResp) :
102273df0db0SJames Feist         asyncResp(asyncResp)
102373df0db0SJames Feist 
10241abe55efSEd Tanous     {
10259c310685SBorawski.Lukasz     }
10269c310685SBorawski.Lukasz 
102773df0db0SJames Feist     void run()
10285b4aa86bSJames Feist     {
102973df0db0SJames Feist         std::shared_ptr<GetPIDValues> self = shared_from_this();
103073df0db0SJames Feist 
103173df0db0SJames Feist         // get all configurations
10325b4aa86bSJames Feist         crow::connections::systemBus->async_method_call(
103373df0db0SJames Feist             [self](const boost::system::error_code ec,
10345b4aa86bSJames Feist                    const crow::openbmc_mapper::GetSubTreeType& subtree) {
10355b4aa86bSJames Feist                 if (ec)
10365b4aa86bSJames Feist                 {
10375b4aa86bSJames Feist                     BMCWEB_LOG_ERROR << ec;
103873df0db0SJames Feist                     messages::internalError(self->asyncResp->res);
103973df0db0SJames Feist                     return;
104073df0db0SJames Feist                 }
104173df0db0SJames Feist                 self->subtree = subtree;
104273df0db0SJames Feist             },
104373df0db0SJames Feist             "xyz.openbmc_project.ObjectMapper",
104473df0db0SJames Feist             "/xyz/openbmc_project/object_mapper",
104573df0db0SJames Feist             "xyz.openbmc_project.ObjectMapper", "GetSubTree", "/", 0,
104673df0db0SJames Feist             std::array<const char*, 4>{
104773df0db0SJames Feist                 pidConfigurationIface, pidZoneConfigurationIface,
104873df0db0SJames Feist                 objectManagerIface, stepwiseConfigurationIface});
104973df0db0SJames Feist 
105073df0db0SJames Feist         // at the same time get the selected profile
105173df0db0SJames Feist         crow::connections::systemBus->async_method_call(
105273df0db0SJames Feist             [self](const boost::system::error_code ec,
105373df0db0SJames Feist                    const crow::openbmc_mapper::GetSubTreeType& subtree) {
105473df0db0SJames Feist                 if (ec || subtree.empty())
105573df0db0SJames Feist                 {
105673df0db0SJames Feist                     return;
105773df0db0SJames Feist                 }
105873df0db0SJames Feist                 if (subtree[0].second.size() != 1)
105973df0db0SJames Feist                 {
106073df0db0SJames Feist                     // invalid mapper response, should never happen
106173df0db0SJames Feist                     BMCWEB_LOG_ERROR << "GetPIDValues: Mapper Error";
106273df0db0SJames Feist                     messages::internalError(self->asyncResp->res);
10635b4aa86bSJames Feist                     return;
10645b4aa86bSJames Feist                 }
10655b4aa86bSJames Feist 
106673df0db0SJames Feist                 const std::string& path = subtree[0].first;
106773df0db0SJames Feist                 const std::string& owner = subtree[0].second[0].first;
106873df0db0SJames Feist                 crow::connections::systemBus->async_method_call(
106973df0db0SJames Feist                     [path, owner, self](
107073df0db0SJames Feist                         const boost::system::error_code ec,
107173df0db0SJames Feist                         const boost::container::flat_map<
107273df0db0SJames Feist                             std::string, std::variant<std::vector<std::string>,
107373df0db0SJames Feist                                                       std::string>>& resp) {
107473df0db0SJames Feist                         if (ec)
107573df0db0SJames Feist                         {
107673df0db0SJames Feist                             BMCWEB_LOG_ERROR << "GetPIDValues: Can't get "
107773df0db0SJames Feist                                                 "thermalModeIface "
107873df0db0SJames Feist                                              << path;
107973df0db0SJames Feist                             messages::internalError(self->asyncResp->res);
108073df0db0SJames Feist                             return;
108173df0db0SJames Feist                         }
1082271584abSEd Tanous                         const std::string* current = nullptr;
1083271584abSEd Tanous                         const std::vector<std::string>* supported = nullptr;
108473df0db0SJames Feist                         for (auto& [key, value] : resp)
108573df0db0SJames Feist                         {
108673df0db0SJames Feist                             if (key == "Current")
108773df0db0SJames Feist                             {
108873df0db0SJames Feist                                 current = std::get_if<std::string>(&value);
108973df0db0SJames Feist                                 if (current == nullptr)
109073df0db0SJames Feist                                 {
109173df0db0SJames Feist                                     BMCWEB_LOG_ERROR
109273df0db0SJames Feist                                         << "GetPIDValues: thermal mode "
109373df0db0SJames Feist                                            "iface invalid "
109473df0db0SJames Feist                                         << path;
109573df0db0SJames Feist                                     messages::internalError(
109673df0db0SJames Feist                                         self->asyncResp->res);
109773df0db0SJames Feist                                     return;
109873df0db0SJames Feist                                 }
109973df0db0SJames Feist                             }
110073df0db0SJames Feist                             if (key == "Supported")
110173df0db0SJames Feist                             {
110273df0db0SJames Feist                                 supported =
110373df0db0SJames Feist                                     std::get_if<std::vector<std::string>>(
110473df0db0SJames Feist                                         &value);
110573df0db0SJames Feist                                 if (supported == nullptr)
110673df0db0SJames Feist                                 {
110773df0db0SJames Feist                                     BMCWEB_LOG_ERROR
110873df0db0SJames Feist                                         << "GetPIDValues: thermal mode "
110973df0db0SJames Feist                                            "iface invalid"
111073df0db0SJames Feist                                         << path;
111173df0db0SJames Feist                                     messages::internalError(
111273df0db0SJames Feist                                         self->asyncResp->res);
111373df0db0SJames Feist                                     return;
111473df0db0SJames Feist                                 }
111573df0db0SJames Feist                             }
111673df0db0SJames Feist                         }
111773df0db0SJames Feist                         if (current == nullptr || supported == nullptr)
111873df0db0SJames Feist                         {
111973df0db0SJames Feist                             BMCWEB_LOG_ERROR << "GetPIDValues: thermal mode "
112073df0db0SJames Feist                                                 "iface invalid "
112173df0db0SJames Feist                                              << path;
112273df0db0SJames Feist                             messages::internalError(self->asyncResp->res);
112373df0db0SJames Feist                             return;
112473df0db0SJames Feist                         }
112573df0db0SJames Feist                         self->currentProfile = *current;
112673df0db0SJames Feist                         self->supportedProfiles = *supported;
112773df0db0SJames Feist                     },
112873df0db0SJames Feist                     owner, path, "org.freedesktop.DBus.Properties", "GetAll",
112973df0db0SJames Feist                     thermalModeIface);
113073df0db0SJames Feist             },
113173df0db0SJames Feist             "xyz.openbmc_project.ObjectMapper",
113273df0db0SJames Feist             "/xyz/openbmc_project/object_mapper",
113373df0db0SJames Feist             "xyz.openbmc_project.ObjectMapper", "GetSubTree", "/", 0,
113473df0db0SJames Feist             std::array<const char*, 1>{thermalModeIface});
113573df0db0SJames Feist     }
113673df0db0SJames Feist 
113773df0db0SJames Feist     ~GetPIDValues()
113873df0db0SJames Feist     {
113973df0db0SJames Feist         if (asyncResp->res.result() != boost::beast::http::status::ok)
114073df0db0SJames Feist         {
114173df0db0SJames Feist             return;
114273df0db0SJames Feist         }
11435b4aa86bSJames Feist         // create map of <connection, path to objMgr>>
114473df0db0SJames Feist         boost::container::flat_map<std::string, std::string> objectMgrPaths;
11456bce33bcSJames Feist         boost::container::flat_set<std::string> calledConnections;
11465b4aa86bSJames Feist         for (const auto& pathGroup : subtree)
11475b4aa86bSJames Feist         {
11485b4aa86bSJames Feist             for (const auto& connectionGroup : pathGroup.second)
11495b4aa86bSJames Feist             {
11506bce33bcSJames Feist                 auto findConnection =
11516bce33bcSJames Feist                     calledConnections.find(connectionGroup.first);
11526bce33bcSJames Feist                 if (findConnection != calledConnections.end())
11536bce33bcSJames Feist                 {
11546bce33bcSJames Feist                     break;
11556bce33bcSJames Feist                 }
115673df0db0SJames Feist                 for (const std::string& interface : connectionGroup.second)
11575b4aa86bSJames Feist                 {
11585b4aa86bSJames Feist                     if (interface == objectManagerIface)
11595b4aa86bSJames Feist                     {
116073df0db0SJames Feist                         objectMgrPaths[connectionGroup.first] = pathGroup.first;
11615b4aa86bSJames Feist                     }
11625b4aa86bSJames Feist                     // this list is alphabetical, so we
11635b4aa86bSJames Feist                     // should have found the objMgr by now
11645b4aa86bSJames Feist                     if (interface == pidConfigurationIface ||
1165b7a08d04SJames Feist                         interface == pidZoneConfigurationIface ||
1166b7a08d04SJames Feist                         interface == stepwiseConfigurationIface)
11675b4aa86bSJames Feist                     {
11685b4aa86bSJames Feist                         auto findObjMgr =
11695b4aa86bSJames Feist                             objectMgrPaths.find(connectionGroup.first);
11705b4aa86bSJames Feist                         if (findObjMgr == objectMgrPaths.end())
11715b4aa86bSJames Feist                         {
11725b4aa86bSJames Feist                             BMCWEB_LOG_DEBUG << connectionGroup.first
11735b4aa86bSJames Feist                                              << "Has no Object Manager";
11745b4aa86bSJames Feist                             continue;
11755b4aa86bSJames Feist                         }
11766bce33bcSJames Feist 
11776bce33bcSJames Feist                         calledConnections.insert(connectionGroup.first);
11786bce33bcSJames Feist 
117973df0db0SJames Feist                         asyncPopulatePid(findObjMgr->first, findObjMgr->second,
118073df0db0SJames Feist                                          currentProfile, supportedProfiles,
118173df0db0SJames Feist                                          asyncResp);
11825b4aa86bSJames Feist                         break;
11835b4aa86bSJames Feist                     }
11845b4aa86bSJames Feist                 }
11855b4aa86bSJames Feist             }
11865b4aa86bSJames Feist         }
118773df0db0SJames Feist     }
118873df0db0SJames Feist 
118973df0db0SJames Feist     std::vector<std::string> supportedProfiles;
119073df0db0SJames Feist     std::string currentProfile;
119173df0db0SJames Feist     crow::openbmc_mapper::GetSubTreeType subtree;
119273df0db0SJames Feist     std::shared_ptr<AsyncResp> asyncResp;
119373df0db0SJames Feist };
119473df0db0SJames Feist 
119573df0db0SJames Feist struct SetPIDValues : std::enable_shared_from_this<SetPIDValues>
119673df0db0SJames Feist {
119773df0db0SJames Feist 
1198271584abSEd Tanous     SetPIDValues(const std::shared_ptr<AsyncResp>& asyncRespIn,
119973df0db0SJames Feist                  nlohmann::json& data) :
1200271584abSEd Tanous         asyncResp(asyncRespIn)
120173df0db0SJames Feist     {
120273df0db0SJames Feist 
120373df0db0SJames Feist         std::optional<nlohmann::json> pidControllers;
120473df0db0SJames Feist         std::optional<nlohmann::json> fanControllers;
120573df0db0SJames Feist         std::optional<nlohmann::json> fanZones;
120673df0db0SJames Feist         std::optional<nlohmann::json> stepwiseControllers;
120773df0db0SJames Feist 
120873df0db0SJames Feist         if (!redfish::json_util::readJson(
120973df0db0SJames Feist                 data, asyncResp->res, "PidControllers", pidControllers,
121073df0db0SJames Feist                 "FanControllers", fanControllers, "FanZones", fanZones,
121173df0db0SJames Feist                 "StepwiseControllers", stepwiseControllers, "Profile", profile))
121273df0db0SJames Feist         {
121373df0db0SJames Feist             BMCWEB_LOG_ERROR << "Line:" << __LINE__ << ", Illegal Property "
121473df0db0SJames Feist                              << data.dump();
121573df0db0SJames Feist             return;
121673df0db0SJames Feist         }
121773df0db0SJames Feist         configuration.emplace_back("PidControllers", std::move(pidControllers));
121873df0db0SJames Feist         configuration.emplace_back("FanControllers", std::move(fanControllers));
121973df0db0SJames Feist         configuration.emplace_back("FanZones", std::move(fanZones));
122073df0db0SJames Feist         configuration.emplace_back("StepwiseControllers",
122173df0db0SJames Feist                                    std::move(stepwiseControllers));
122273df0db0SJames Feist     }
122373df0db0SJames Feist     void run()
122473df0db0SJames Feist     {
122573df0db0SJames Feist         if (asyncResp->res.result() != boost::beast::http::status::ok)
122673df0db0SJames Feist         {
122773df0db0SJames Feist             return;
122873df0db0SJames Feist         }
122973df0db0SJames Feist 
123073df0db0SJames Feist         std::shared_ptr<SetPIDValues> self = shared_from_this();
123173df0db0SJames Feist 
123273df0db0SJames Feist         // todo(james): might make sense to do a mapper call here if this
123373df0db0SJames Feist         // interface gets more traction
123473df0db0SJames Feist         crow::connections::systemBus->async_method_call(
123573df0db0SJames Feist             [self](const boost::system::error_code ec,
1236271584abSEd Tanous                    dbus::utility::ManagedObjectType& mObj) {
123773df0db0SJames Feist                 if (ec)
123873df0db0SJames Feist                 {
123973df0db0SJames Feist                     BMCWEB_LOG_ERROR << "Error communicating to Entity Manager";
124073df0db0SJames Feist                     messages::internalError(self->asyncResp->res);
124173df0db0SJames Feist                     return;
124273df0db0SJames Feist                 }
1243271584abSEd Tanous                 self->managedObj = std::move(mObj);
124473df0db0SJames Feist             },
124573df0db0SJames Feist             "xyz.openbmc_project.EntityManager", "/", objectManagerIface,
124673df0db0SJames Feist             "GetManagedObjects");
124773df0db0SJames Feist 
124873df0db0SJames Feist         // at the same time get the profile information
124973df0db0SJames Feist         crow::connections::systemBus->async_method_call(
125073df0db0SJames Feist             [self](const boost::system::error_code ec,
125173df0db0SJames Feist                    const crow::openbmc_mapper::GetSubTreeType& subtree) {
125273df0db0SJames Feist                 if (ec || subtree.empty())
125373df0db0SJames Feist                 {
125473df0db0SJames Feist                     return;
125573df0db0SJames Feist                 }
125673df0db0SJames Feist                 if (subtree[0].second.empty())
125773df0db0SJames Feist                 {
125873df0db0SJames Feist                     // invalid mapper response, should never happen
125973df0db0SJames Feist                     BMCWEB_LOG_ERROR << "SetPIDValues: Mapper Error";
126073df0db0SJames Feist                     messages::internalError(self->asyncResp->res);
126173df0db0SJames Feist                     return;
126273df0db0SJames Feist                 }
126373df0db0SJames Feist 
126473df0db0SJames Feist                 const std::string& path = subtree[0].first;
126573df0db0SJames Feist                 const std::string& owner = subtree[0].second[0].first;
126673df0db0SJames Feist                 crow::connections::systemBus->async_method_call(
126773df0db0SJames Feist                     [self, path, owner](
126873df0db0SJames Feist                         const boost::system::error_code ec,
126973df0db0SJames Feist                         const boost::container::flat_map<
127073df0db0SJames Feist                             std::string, std::variant<std::vector<std::string>,
1271271584abSEd Tanous                                                       std::string>>& r) {
127273df0db0SJames Feist                         if (ec)
127373df0db0SJames Feist                         {
127473df0db0SJames Feist                             BMCWEB_LOG_ERROR << "SetPIDValues: Can't get "
127573df0db0SJames Feist                                                 "thermalModeIface "
127673df0db0SJames Feist                                              << path;
127773df0db0SJames Feist                             messages::internalError(self->asyncResp->res);
127873df0db0SJames Feist                             return;
127973df0db0SJames Feist                         }
1280271584abSEd Tanous                         const std::string* current = nullptr;
1281271584abSEd Tanous                         const std::vector<std::string>* supported = nullptr;
1282271584abSEd Tanous                         for (auto& [key, value] : r)
128373df0db0SJames Feist                         {
128473df0db0SJames Feist                             if (key == "Current")
128573df0db0SJames Feist                             {
128673df0db0SJames Feist                                 current = std::get_if<std::string>(&value);
128773df0db0SJames Feist                                 if (current == nullptr)
128873df0db0SJames Feist                                 {
128973df0db0SJames Feist                                     BMCWEB_LOG_ERROR
129073df0db0SJames Feist                                         << "SetPIDValues: thermal mode "
129173df0db0SJames Feist                                            "iface invalid "
129273df0db0SJames Feist                                         << path;
129373df0db0SJames Feist                                     messages::internalError(
129473df0db0SJames Feist                                         self->asyncResp->res);
129573df0db0SJames Feist                                     return;
129673df0db0SJames Feist                                 }
129773df0db0SJames Feist                             }
129873df0db0SJames Feist                             if (key == "Supported")
129973df0db0SJames Feist                             {
130073df0db0SJames Feist                                 supported =
130173df0db0SJames Feist                                     std::get_if<std::vector<std::string>>(
130273df0db0SJames Feist                                         &value);
130373df0db0SJames Feist                                 if (supported == nullptr)
130473df0db0SJames Feist                                 {
130573df0db0SJames Feist                                     BMCWEB_LOG_ERROR
130673df0db0SJames Feist                                         << "SetPIDValues: thermal mode "
130773df0db0SJames Feist                                            "iface invalid"
130873df0db0SJames Feist                                         << path;
130973df0db0SJames Feist                                     messages::internalError(
131073df0db0SJames Feist                                         self->asyncResp->res);
131173df0db0SJames Feist                                     return;
131273df0db0SJames Feist                                 }
131373df0db0SJames Feist                             }
131473df0db0SJames Feist                         }
131573df0db0SJames Feist                         if (current == nullptr || supported == nullptr)
131673df0db0SJames Feist                         {
131773df0db0SJames Feist                             BMCWEB_LOG_ERROR << "SetPIDValues: thermal mode "
131873df0db0SJames Feist                                                 "iface invalid "
131973df0db0SJames Feist                                              << path;
132073df0db0SJames Feist                             messages::internalError(self->asyncResp->res);
132173df0db0SJames Feist                             return;
132273df0db0SJames Feist                         }
132373df0db0SJames Feist                         self->currentProfile = *current;
132473df0db0SJames Feist                         self->supportedProfiles = *supported;
132573df0db0SJames Feist                         self->profileConnection = owner;
132673df0db0SJames Feist                         self->profilePath = path;
132773df0db0SJames Feist                     },
132873df0db0SJames Feist                     owner, path, "org.freedesktop.DBus.Properties", "GetAll",
132973df0db0SJames Feist                     thermalModeIface);
13305b4aa86bSJames Feist             },
13315b4aa86bSJames Feist             "xyz.openbmc_project.ObjectMapper",
13325b4aa86bSJames Feist             "/xyz/openbmc_project/object_mapper",
13335b4aa86bSJames Feist             "xyz.openbmc_project.ObjectMapper", "GetSubTree", "/", 0,
133473df0db0SJames Feist             std::array<const char*, 1>{thermalModeIface});
133573df0db0SJames Feist     }
133673df0db0SJames Feist     ~SetPIDValues()
133773df0db0SJames Feist     {
133873df0db0SJames Feist         if (asyncResp->res.result() != boost::beast::http::status::ok)
133973df0db0SJames Feist         {
134073df0db0SJames Feist             return;
13415b4aa86bSJames Feist         }
13425b4aa86bSJames Feist 
134373df0db0SJames Feist         std::shared_ptr<AsyncResp> response = asyncResp;
134473df0db0SJames Feist 
134573df0db0SJames Feist         if (profile)
134673df0db0SJames Feist         {
134773df0db0SJames Feist             if (std::find(supportedProfiles.begin(), supportedProfiles.end(),
134873df0db0SJames Feist                           *profile) == supportedProfiles.end())
134973df0db0SJames Feist             {
135073df0db0SJames Feist                 messages::actionParameterUnknown(response->res, "Profile",
135173df0db0SJames Feist                                                  *profile);
135273df0db0SJames Feist                 return;
135373df0db0SJames Feist             }
135473df0db0SJames Feist             currentProfile = *profile;
135573df0db0SJames Feist             crow::connections::systemBus->async_method_call(
135673df0db0SJames Feist                 [response](const boost::system::error_code ec) {
135773df0db0SJames Feist                     if (ec)
135873df0db0SJames Feist                     {
135973df0db0SJames Feist                         BMCWEB_LOG_ERROR << "Error patching profile" << ec;
136073df0db0SJames Feist                         messages::internalError(response->res);
136173df0db0SJames Feist                     }
136273df0db0SJames Feist                 },
136373df0db0SJames Feist                 profileConnection, profilePath,
136473df0db0SJames Feist                 "org.freedesktop.DBus.Properties", "Set", thermalModeIface,
136573df0db0SJames Feist                 "Current", std::variant<std::string>(*profile));
136673df0db0SJames Feist         }
136773df0db0SJames Feist 
136873df0db0SJames Feist         for (auto& containerPair : configuration)
136973df0db0SJames Feist         {
137073df0db0SJames Feist             auto& container = containerPair.second;
137173df0db0SJames Feist             if (!container)
137273df0db0SJames Feist             {
137373df0db0SJames Feist                 continue;
137473df0db0SJames Feist             }
1375*6ee7f774SJames Feist             BMCWEB_LOG_DEBUG << *container;
1376*6ee7f774SJames Feist 
137773df0db0SJames Feist             std::string& type = containerPair.first;
137873df0db0SJames Feist 
137973df0db0SJames Feist             for (nlohmann::json::iterator it = container->begin();
138073df0db0SJames Feist                  it != container->end(); it++)
138173df0db0SJames Feist             {
138273df0db0SJames Feist                 const auto& name = it.key();
1383*6ee7f774SJames Feist                 BMCWEB_LOG_DEBUG << "looking for " << name;
1384*6ee7f774SJames Feist 
138573df0db0SJames Feist                 auto pathItr =
138673df0db0SJames Feist                     std::find_if(managedObj.begin(), managedObj.end(),
138773df0db0SJames Feist                                  [&name](const auto& obj) {
138873df0db0SJames Feist                                      return boost::algorithm::ends_with(
138973df0db0SJames Feist                                          obj.first.str, "/" + name);
139073df0db0SJames Feist                                  });
139173df0db0SJames Feist                 boost::container::flat_map<std::string,
139273df0db0SJames Feist                                            dbus::utility::DbusVariantType>
139373df0db0SJames Feist                     output;
139473df0db0SJames Feist 
139573df0db0SJames Feist                 output.reserve(16); // The pid interface length
139673df0db0SJames Feist 
139773df0db0SJames Feist                 // determines if we're patching entity-manager or
139873df0db0SJames Feist                 // creating a new object
139973df0db0SJames Feist                 bool createNewObject = (pathItr == managedObj.end());
1400*6ee7f774SJames Feist                 BMCWEB_LOG_DEBUG << "Found = " << !createNewObject;
1401*6ee7f774SJames Feist 
140273df0db0SJames Feist                 std::string iface;
140373df0db0SJames Feist                 if (type == "PidControllers" || type == "FanControllers")
140473df0db0SJames Feist                 {
140573df0db0SJames Feist                     iface = pidConfigurationIface;
140673df0db0SJames Feist                     if (!createNewObject &&
140773df0db0SJames Feist                         pathItr->second.find(pidConfigurationIface) ==
140873df0db0SJames Feist                             pathItr->second.end())
140973df0db0SJames Feist                     {
141073df0db0SJames Feist                         createNewObject = true;
141173df0db0SJames Feist                     }
141273df0db0SJames Feist                 }
141373df0db0SJames Feist                 else if (type == "FanZones")
141473df0db0SJames Feist                 {
141573df0db0SJames Feist                     iface = pidZoneConfigurationIface;
141673df0db0SJames Feist                     if (!createNewObject &&
141773df0db0SJames Feist                         pathItr->second.find(pidZoneConfigurationIface) ==
141873df0db0SJames Feist                             pathItr->second.end())
141973df0db0SJames Feist                     {
142073df0db0SJames Feist 
142173df0db0SJames Feist                         createNewObject = true;
142273df0db0SJames Feist                     }
142373df0db0SJames Feist                 }
142473df0db0SJames Feist                 else if (type == "StepwiseControllers")
142573df0db0SJames Feist                 {
142673df0db0SJames Feist                     iface = stepwiseConfigurationIface;
142773df0db0SJames Feist                     if (!createNewObject &&
142873df0db0SJames Feist                         pathItr->second.find(stepwiseConfigurationIface) ==
142973df0db0SJames Feist                             pathItr->second.end())
143073df0db0SJames Feist                     {
143173df0db0SJames Feist                         createNewObject = true;
143273df0db0SJames Feist                     }
143373df0db0SJames Feist                 }
1434*6ee7f774SJames Feist 
1435*6ee7f774SJames Feist                 if (createNewObject && it.value() == nullptr)
1436*6ee7f774SJames Feist                 {
1437*6ee7f774SJames Feist                     // can't delete a non-existant object
1438*6ee7f774SJames Feist                     messages::invalidObject(response->res, name);
1439*6ee7f774SJames Feist                     continue;
1440*6ee7f774SJames Feist                 }
1441*6ee7f774SJames Feist 
1442*6ee7f774SJames Feist                 std::string path;
1443*6ee7f774SJames Feist                 if (pathItr != managedObj.end())
1444*6ee7f774SJames Feist                 {
1445*6ee7f774SJames Feist                     path = pathItr->first.str;
1446*6ee7f774SJames Feist                 }
1447*6ee7f774SJames Feist 
144873df0db0SJames Feist                 BMCWEB_LOG_DEBUG << "Create new = " << createNewObject << "\n";
144973df0db0SJames Feist                 output["Name"] = boost::replace_all_copy(name, "_", " ");
145073df0db0SJames Feist 
145173df0db0SJames Feist                 std::string chassis;
145273df0db0SJames Feist                 CreatePIDRet ret = createPidInterface(
1453*6ee7f774SJames Feist                     response, type, it, path, managedObj, createNewObject,
1454*6ee7f774SJames Feist                     output, chassis, currentProfile);
145573df0db0SJames Feist                 if (ret == CreatePIDRet::fail)
145673df0db0SJames Feist                 {
145773df0db0SJames Feist                     return;
145873df0db0SJames Feist                 }
145973df0db0SJames Feist                 else if (ret == CreatePIDRet::del)
146073df0db0SJames Feist                 {
146173df0db0SJames Feist                     continue;
146273df0db0SJames Feist                 }
146373df0db0SJames Feist 
146473df0db0SJames Feist                 if (!createNewObject)
146573df0db0SJames Feist                 {
146673df0db0SJames Feist                     for (const auto& property : output)
146773df0db0SJames Feist                     {
146873df0db0SJames Feist                         crow::connections::systemBus->async_method_call(
146973df0db0SJames Feist                             [response,
147073df0db0SJames Feist                              propertyName{std::string(property.first)}](
147173df0db0SJames Feist                                 const boost::system::error_code ec) {
147273df0db0SJames Feist                                 if (ec)
147373df0db0SJames Feist                                 {
147473df0db0SJames Feist                                     BMCWEB_LOG_ERROR << "Error patching "
147573df0db0SJames Feist                                                      << propertyName << ": "
147673df0db0SJames Feist                                                      << ec;
147773df0db0SJames Feist                                     messages::internalError(response->res);
147873df0db0SJames Feist                                     return;
147973df0db0SJames Feist                                 }
148073df0db0SJames Feist                                 messages::success(response->res);
148173df0db0SJames Feist                             },
1482*6ee7f774SJames Feist                             "xyz.openbmc_project.EntityManager", path,
148373df0db0SJames Feist                             "org.freedesktop.DBus.Properties", "Set", iface,
148473df0db0SJames Feist                             property.first, property.second);
148573df0db0SJames Feist                     }
148673df0db0SJames Feist                 }
148773df0db0SJames Feist                 else
148873df0db0SJames Feist                 {
148973df0db0SJames Feist                     if (chassis.empty())
149073df0db0SJames Feist                     {
149173df0db0SJames Feist                         BMCWEB_LOG_ERROR << "Failed to get chassis from config";
149273df0db0SJames Feist                         messages::invalidObject(response->res, name);
149373df0db0SJames Feist                         return;
149473df0db0SJames Feist                     }
149573df0db0SJames Feist 
149673df0db0SJames Feist                     bool foundChassis = false;
149773df0db0SJames Feist                     for (const auto& obj : managedObj)
149873df0db0SJames Feist                     {
149973df0db0SJames Feist                         if (boost::algorithm::ends_with(obj.first.str, chassis))
150073df0db0SJames Feist                         {
150173df0db0SJames Feist                             chassis = obj.first.str;
150273df0db0SJames Feist                             foundChassis = true;
150373df0db0SJames Feist                             break;
150473df0db0SJames Feist                         }
150573df0db0SJames Feist                     }
150673df0db0SJames Feist                     if (!foundChassis)
150773df0db0SJames Feist                     {
150873df0db0SJames Feist                         BMCWEB_LOG_ERROR << "Failed to find chassis on dbus";
150973df0db0SJames Feist                         messages::resourceMissingAtURI(
151073df0db0SJames Feist                             response->res, "/redfish/v1/Chassis/" + chassis);
151173df0db0SJames Feist                         return;
151273df0db0SJames Feist                     }
151373df0db0SJames Feist 
151473df0db0SJames Feist                     crow::connections::systemBus->async_method_call(
151573df0db0SJames Feist                         [response](const boost::system::error_code ec) {
151673df0db0SJames Feist                             if (ec)
151773df0db0SJames Feist                             {
151873df0db0SJames Feist                                 BMCWEB_LOG_ERROR << "Error Adding Pid Object "
151973df0db0SJames Feist                                                  << ec;
152073df0db0SJames Feist                                 messages::internalError(response->res);
152173df0db0SJames Feist                                 return;
152273df0db0SJames Feist                             }
152373df0db0SJames Feist                             messages::success(response->res);
152473df0db0SJames Feist                         },
152573df0db0SJames Feist                         "xyz.openbmc_project.EntityManager", chassis,
152673df0db0SJames Feist                         "xyz.openbmc_project.AddObject", "AddObject", output);
152773df0db0SJames Feist                 }
152873df0db0SJames Feist             }
152973df0db0SJames Feist         }
153073df0db0SJames Feist     }
153173df0db0SJames Feist     std::shared_ptr<AsyncResp> asyncResp;
153273df0db0SJames Feist     std::vector<std::pair<std::string, std::optional<nlohmann::json>>>
153373df0db0SJames Feist         configuration;
153473df0db0SJames Feist     std::optional<std::string> profile;
153573df0db0SJames Feist     dbus::utility::ManagedObjectType managedObj;
153673df0db0SJames Feist     std::vector<std::string> supportedProfiles;
153773df0db0SJames Feist     std::string currentProfile;
153873df0db0SJames Feist     std::string profileConnection;
153973df0db0SJames Feist     std::string profilePath;
154073df0db0SJames Feist };
154173df0db0SJames Feist 
154273df0db0SJames Feist class Manager : public Node
154373df0db0SJames Feist {
154473df0db0SJames Feist   public:
154573df0db0SJames Feist     Manager(CrowApp& app) : Node(app, "/redfish/v1/Managers/bmc/")
154673df0db0SJames Feist     {
154773df0db0SJames Feist         uuid = app.template getMiddleware<crow::persistent_data::Middleware>()
154873df0db0SJames Feist                    .systemUuid;
154973df0db0SJames Feist         entityPrivileges = {
155073df0db0SJames Feist             {boost::beast::http::verb::get, {{"Login"}}},
155173df0db0SJames Feist             {boost::beast::http::verb::head, {{"Login"}}},
155273df0db0SJames Feist             {boost::beast::http::verb::patch, {{"ConfigureManager"}}},
155373df0db0SJames Feist             {boost::beast::http::verb::put, {{"ConfigureManager"}}},
155473df0db0SJames Feist             {boost::beast::http::verb::delete_, {{"ConfigureManager"}}},
155573df0db0SJames Feist             {boost::beast::http::verb::post, {{"ConfigureManager"}}}};
155673df0db0SJames Feist     }
155773df0db0SJames Feist 
155873df0db0SJames Feist   private:
155955c7b7a2SEd Tanous     void doGet(crow::Response& res, const crow::Request& req,
15601abe55efSEd Tanous                const std::vector<std::string>& params) override
15611abe55efSEd Tanous     {
15620f74e643SEd Tanous         res.jsonValue["@odata.id"] = "/redfish/v1/Managers/bmc";
15630f74e643SEd Tanous         res.jsonValue["@odata.type"] = "#Manager.v1_3_0.Manager";
15640f74e643SEd Tanous         res.jsonValue["@odata.context"] =
15650f74e643SEd Tanous             "/redfish/v1/$metadata#Manager.Manager";
15660f74e643SEd Tanous         res.jsonValue["Id"] = "bmc";
15670f74e643SEd Tanous         res.jsonValue["Name"] = "OpenBmc Manager";
15680f74e643SEd Tanous         res.jsonValue["Description"] = "Baseboard Management Controller";
15690f74e643SEd Tanous         res.jsonValue["PowerState"] = "On";
1570029573d4SEd Tanous         res.jsonValue["Status"] = {{"State", "Enabled"}, {"Health", "OK"}};
15710f74e643SEd Tanous         res.jsonValue["ManagerType"] = "BMC";
15723602e232SEd Tanous         res.jsonValue["UUID"] = systemd_utils::getUuid();
15733602e232SEd Tanous         res.jsonValue["ServiceEntryPointUUID"] = uuid;
15740f74e643SEd Tanous         res.jsonValue["Model"] = "OpenBmc"; // TODO(ed), get model
15750f74e643SEd Tanous 
15760f74e643SEd Tanous         res.jsonValue["LogServices"] = {
15770f74e643SEd Tanous             {"@odata.id", "/redfish/v1/Managers/bmc/LogServices"}};
15780f74e643SEd Tanous 
15790f74e643SEd Tanous         res.jsonValue["NetworkProtocol"] = {
15800f74e643SEd Tanous             {"@odata.id", "/redfish/v1/Managers/bmc/NetworkProtocol"}};
15810f74e643SEd Tanous 
15820f74e643SEd Tanous         res.jsonValue["EthernetInterfaces"] = {
15830f74e643SEd Tanous             {"@odata.id", "/redfish/v1/Managers/bmc/EthernetInterfaces"}};
1584107077deSPrzemyslaw Czarnowski 
1585107077deSPrzemyslaw Czarnowski #ifdef BMCWEB_ENABLE_VM_NBDPROXY
1586107077deSPrzemyslaw Czarnowski         res.jsonValue["VirtualMedia"] = {
1587107077deSPrzemyslaw Czarnowski             {"@odata.id", "/redfish/v1/Managers/bmc/VirtualMedia"}};
1588107077deSPrzemyslaw Czarnowski #endif // BMCWEB_ENABLE_VM_NBDPROXY
1589107077deSPrzemyslaw Czarnowski 
15900f74e643SEd Tanous         // default oem data
15910f74e643SEd Tanous         nlohmann::json& oem = res.jsonValue["Oem"];
15920f74e643SEd Tanous         nlohmann::json& oemOpenbmc = oem["OpenBmc"];
15930f74e643SEd Tanous         oem["@odata.type"] = "#OemManager.Oem";
15940f74e643SEd Tanous         oem["@odata.id"] = "/redfish/v1/Managers/bmc#/Oem";
15950f74e643SEd Tanous         oem["@odata.context"] = "/redfish/v1/$metadata#OemManager.Oem";
15960f74e643SEd Tanous         oemOpenbmc["@odata.type"] = "#OemManager.OpenBmc";
15970f74e643SEd Tanous         oemOpenbmc["@odata.id"] = "/redfish/v1/Managers/bmc#/Oem/OpenBmc";
15980f74e643SEd Tanous         oemOpenbmc["@odata.context"] =
15990f74e643SEd Tanous             "/redfish/v1/$metadata#OemManager.OpenBmc";
1600cfcd5f6bSMarri Devender Rao         oemOpenbmc["Certificates"] = {
1601cfcd5f6bSMarri Devender Rao             {"@odata.id", "/redfish/v1/Managers/bmc/Truststore/Certificates"}};
16020f74e643SEd Tanous 
1603ed5befbdSJennifer Lee         // Update Actions object.
16040f74e643SEd Tanous         nlohmann::json& manager_reset =
16050f74e643SEd Tanous             res.jsonValue["Actions"]["#Manager.Reset"];
1606ed5befbdSJennifer Lee         manager_reset["target"] =
1607ed5befbdSJennifer Lee             "/redfish/v1/Managers/bmc/Actions/Manager.Reset";
1608ed5befbdSJennifer Lee         manager_reset["ResetType@Redfish.AllowableValues"] = {
1609ed5befbdSJennifer Lee             "GracefulRestart"};
1610ca537928SJennifer Lee 
1611cb92c03bSAndrew Geissler         res.jsonValue["DateTime"] = crow::utility::dateTimeNow();
1612474bfad5SSantosh Puranik 
1613f8c3e6f0SKuiying Wang         // Fill in SerialConsole info
1614474bfad5SSantosh Puranik         res.jsonValue["SerialConsole"]["ServiceEnabled"] = true;
1615f8c3e6f0SKuiying Wang         res.jsonValue["SerialConsole"]["MaxConcurrentSessions"] = 15;
1616474bfad5SSantosh Puranik         res.jsonValue["SerialConsole"]["ConnectTypesSupported"] = {"IPMI",
1617474bfad5SSantosh Puranik                                                                    "SSH"};
1618ef47bb18SSantosh Puranik #ifdef BMCWEB_ENABLE_KVM
1619f8c3e6f0SKuiying Wang         // Fill in GraphicalConsole info
1620ef47bb18SSantosh Puranik         res.jsonValue["GraphicalConsole"]["ServiceEnabled"] = true;
1621704fae6cSJae Hyun Yoo         res.jsonValue["GraphicalConsole"]["MaxConcurrentSessions"] = 4;
1622ef47bb18SSantosh Puranik         res.jsonValue["GraphicalConsole"]["ConnectTypesSupported"] = {"KVMIP"};
1623ef47bb18SSantosh Puranik #endif // BMCWEB_ENABLE_KVM
1624474bfad5SSantosh Puranik 
1625603a6640SGunnar Mills         res.jsonValue["Links"]["ManagerForServers@odata.count"] = 1;
1626603a6640SGunnar Mills         res.jsonValue["Links"]["ManagerForServers"] = {
1627603a6640SGunnar Mills             {{"@odata.id", "/redfish/v1/Systems/system"}}};
162826f03899SShawn McCarney 
1629ed5befbdSJennifer Lee         std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
16305b4aa86bSJames Feist 
1631b49ac873SJames Feist         auto health = std::make_shared<HealthPopulate>(asyncResp);
1632b49ac873SJames Feist         health->isManagersHealth = true;
1633b49ac873SJames Feist         health->populate();
1634b49ac873SJames Feist 
1635e90c5052SAndrew Geissler         fw_util::getActiveFwVersion(asyncResp, fw_util::bmcPurpose,
1636e90c5052SAndrew Geissler                                     "FirmwareVersion");
16370f6b00bdSJames Feist 
163873df0db0SJames Feist         auto pids = std::make_shared<GetPIDValues>(asyncResp);
163973df0db0SJames Feist         pids->run();
1640c5d03ff4SJennifer Lee 
1641c5d03ff4SJennifer Lee         getMainChassisId(asyncResp, [](const std::string& chassisId,
1642c5d03ff4SJennifer Lee                                        const std::shared_ptr<AsyncResp> aRsp) {
1643c5d03ff4SJennifer Lee             aRsp->res.jsonValue["Links"]["ManagerForChassis@odata.count"] = 1;
1644c5d03ff4SJennifer Lee             aRsp->res.jsonValue["Links"]["ManagerForChassis"] = {
1645c5d03ff4SJennifer Lee                 {{"@odata.id", "/redfish/v1/Chassis/" + chassisId}}};
16462c0feb00SJason M. Bills             aRsp->res.jsonValue["Links"]["ManagerInChassis"] = {
16472c0feb00SJason M. Bills                 {"@odata.id", "/redfish/v1/Chassis/" + chassisId}};
1648c5d03ff4SJennifer Lee         });
16490f6b00bdSJames Feist 
16500f6b00bdSJames Feist         static bool started = false;
16510f6b00bdSJames Feist 
16520f6b00bdSJames Feist         if (!started)
16530f6b00bdSJames Feist         {
16540f6b00bdSJames Feist             crow::connections::systemBus->async_method_call(
16550f6b00bdSJames Feist                 [asyncResp](const boost::system::error_code ec,
16560f6b00bdSJames Feist                             const std::variant<double>& resp) {
16570f6b00bdSJames Feist                     if (ec)
16580f6b00bdSJames Feist                     {
16590f6b00bdSJames Feist                         BMCWEB_LOG_ERROR << "Error while getting progress";
16600f6b00bdSJames Feist                         messages::internalError(asyncResp->res);
16610f6b00bdSJames Feist                         return;
16620f6b00bdSJames Feist                     }
16630f6b00bdSJames Feist                     const double* val = std::get_if<double>(&resp);
16640f6b00bdSJames Feist                     if (val == nullptr)
16650f6b00bdSJames Feist                     {
16660f6b00bdSJames Feist                         BMCWEB_LOG_ERROR
16670f6b00bdSJames Feist                             << "Invalid response while getting progress";
16680f6b00bdSJames Feist                         messages::internalError(asyncResp->res);
16690f6b00bdSJames Feist                         return;
16700f6b00bdSJames Feist                     }
16710f6b00bdSJames Feist                     if (*val < 1.0)
16720f6b00bdSJames Feist                     {
16730f6b00bdSJames Feist                         asyncResp->res.jsonValue["Status"]["State"] =
16740f6b00bdSJames Feist                             "Starting";
16750f6b00bdSJames Feist                         started = true;
16760f6b00bdSJames Feist                     }
16770f6b00bdSJames Feist                 },
16780f6b00bdSJames Feist                 "org.freedesktop.systemd1", "/org/freedesktop/systemd1",
16790f6b00bdSJames Feist                 "org.freedesktop.DBus.Properties", "Get",
16800f6b00bdSJames Feist                 "org.freedesktop.systemd1.Manager", "Progress");
16810f6b00bdSJames Feist         }
168283ff9ab6SJames Feist     }
16835b4aa86bSJames Feist 
16845b4aa86bSJames Feist     void doPatch(crow::Response& res, const crow::Request& req,
16855b4aa86bSJames Feist                  const std::vector<std::string>& params) override
16865b4aa86bSJames Feist     {
16870627a2c7SEd Tanous         std::optional<nlohmann::json> oem;
1688af5d6058SSantosh Puranik         std::optional<std::string> datetime;
168941352c24SSantosh Puranik         std::shared_ptr<AsyncResp> response = std::make_shared<AsyncResp>(res);
16900627a2c7SEd Tanous 
169141352c24SSantosh Puranik         if (!json_util::readJson(req, response->res, "Oem", oem, "DateTime",
169241352c24SSantosh Puranik                                  datetime))
169383ff9ab6SJames Feist         {
169483ff9ab6SJames Feist             return;
169583ff9ab6SJames Feist         }
16960627a2c7SEd Tanous 
16970627a2c7SEd Tanous         if (oem)
169883ff9ab6SJames Feist         {
16995f2caaefSJames Feist             std::optional<nlohmann::json> openbmc;
170043b761d0SEd Tanous             if (!redfish::json_util::readJson(*oem, res, "OpenBmc", openbmc))
170183ff9ab6SJames Feist             {
170243b761d0SEd Tanous                 BMCWEB_LOG_ERROR << "Line:" << __LINE__ << ", Illegal Property "
170343b761d0SEd Tanous                                  << oem->dump();
170483ff9ab6SJames Feist                 return;
170583ff9ab6SJames Feist             }
17065f2caaefSJames Feist             if (openbmc)
170783ff9ab6SJames Feist             {
17085f2caaefSJames Feist                 std::optional<nlohmann::json> fan;
170943b761d0SEd Tanous                 if (!redfish::json_util::readJson(*openbmc, res, "Fan", fan))
171083ff9ab6SJames Feist                 {
17115f2caaefSJames Feist                     BMCWEB_LOG_ERROR << "Line:" << __LINE__
17125f2caaefSJames Feist                                      << ", Illegal Property "
17135f2caaefSJames Feist                                      << openbmc->dump();
171483ff9ab6SJames Feist                     return;
171583ff9ab6SJames Feist                 }
17165f2caaefSJames Feist                 if (fan)
171783ff9ab6SJames Feist                 {
171873df0db0SJames Feist                     auto pid = std::make_shared<SetPIDValues>(response, *fan);
171973df0db0SJames Feist                     pid->run();
172083ff9ab6SJames Feist                 }
172183ff9ab6SJames Feist             }
172283ff9ab6SJames Feist         }
1723af5d6058SSantosh Puranik         if (datetime)
1724af5d6058SSantosh Puranik         {
1725af5d6058SSantosh Puranik             setDateTime(response, std::move(*datetime));
1726af5d6058SSantosh Puranik         }
1727af5d6058SSantosh Puranik     }
1728af5d6058SSantosh Puranik 
1729af5d6058SSantosh Puranik     void setDateTime(std::shared_ptr<AsyncResp> aResp,
1730af5d6058SSantosh Puranik                      std::string datetime) const
1731af5d6058SSantosh Puranik     {
1732af5d6058SSantosh Puranik         BMCWEB_LOG_DEBUG << "Set date time: " << datetime;
1733af5d6058SSantosh Puranik 
1734af5d6058SSantosh Puranik         std::stringstream stream(datetime);
1735af5d6058SSantosh Puranik         // Convert from ISO 8601 to boost local_time
1736af5d6058SSantosh Puranik         // (BMC only has time in UTC)
1737af5d6058SSantosh Puranik         boost::posix_time::ptime posixTime;
1738af5d6058SSantosh Puranik         boost::posix_time::ptime epoch(boost::gregorian::date(1970, 1, 1));
1739af5d6058SSantosh Puranik         // Facet gets deleted with the stringsteam
1740af5d6058SSantosh Puranik         auto ifc = std::make_unique<boost::local_time::local_time_input_facet>(
1741af5d6058SSantosh Puranik             "%Y-%m-%d %H:%M:%S%F %ZP");
1742af5d6058SSantosh Puranik         stream.imbue(std::locale(stream.getloc(), ifc.release()));
1743af5d6058SSantosh Puranik 
1744af5d6058SSantosh Puranik         boost::local_time::local_date_time ldt(
1745af5d6058SSantosh Puranik             boost::local_time::not_a_date_time);
1746af5d6058SSantosh Puranik 
1747af5d6058SSantosh Puranik         if (stream >> ldt)
1748af5d6058SSantosh Puranik         {
1749af5d6058SSantosh Puranik             posixTime = ldt.utc_time();
1750af5d6058SSantosh Puranik             boost::posix_time::time_duration dur = posixTime - epoch;
1751af5d6058SSantosh Puranik             uint64_t durMicroSecs =
1752af5d6058SSantosh Puranik                 static_cast<uint64_t>(dur.total_microseconds());
1753af5d6058SSantosh Puranik             crow::connections::systemBus->async_method_call(
1754af5d6058SSantosh Puranik                 [aResp{std::move(aResp)}, datetime{std::move(datetime)}](
1755af5d6058SSantosh Puranik                     const boost::system::error_code ec) {
1756af5d6058SSantosh Puranik                     if (ec)
1757af5d6058SSantosh Puranik                     {
1758af5d6058SSantosh Puranik                         BMCWEB_LOG_DEBUG << "Failed to set elapsed time. "
1759af5d6058SSantosh Puranik                                             "DBUS response error "
1760af5d6058SSantosh Puranik                                          << ec;
1761af5d6058SSantosh Puranik                         messages::internalError(aResp->res);
1762af5d6058SSantosh Puranik                         return;
1763af5d6058SSantosh Puranik                     }
1764af5d6058SSantosh Puranik                     aResp->res.jsonValue["DateTime"] = datetime;
1765af5d6058SSantosh Puranik                 },
1766af5d6058SSantosh Puranik                 "xyz.openbmc_project.Time.Manager",
1767af5d6058SSantosh Puranik                 "/xyz/openbmc_project/time/bmc",
1768af5d6058SSantosh Puranik                 "org.freedesktop.DBus.Properties", "Set",
1769af5d6058SSantosh Puranik                 "xyz.openbmc_project.Time.EpochTime", "Elapsed",
1770af5d6058SSantosh Puranik                 std::variant<uint64_t>(durMicroSecs));
1771af5d6058SSantosh Puranik         }
1772af5d6058SSantosh Puranik         else
1773af5d6058SSantosh Puranik         {
1774af5d6058SSantosh Puranik             messages::propertyValueFormatError(aResp->res, datetime,
1775af5d6058SSantosh Puranik                                                "DateTime");
1776af5d6058SSantosh Puranik             return;
1777af5d6058SSantosh Puranik         }
177883ff9ab6SJames Feist     }
17799c310685SBorawski.Lukasz 
17800f74e643SEd Tanous     std::string uuid;
17819c310685SBorawski.Lukasz };
17829c310685SBorawski.Lukasz 
17831abe55efSEd Tanous class ManagerCollection : public Node
17841abe55efSEd Tanous {
17859c310685SBorawski.Lukasz   public:
17861abe55efSEd Tanous     ManagerCollection(CrowApp& app) : Node(app, "/redfish/v1/Managers/")
17871abe55efSEd Tanous     {
1788a434f2bdSEd Tanous         entityPrivileges = {
1789a434f2bdSEd Tanous             {boost::beast::http::verb::get, {{"Login"}}},
1790e0d918bcSEd Tanous             {boost::beast::http::verb::head, {{"Login"}}},
1791e0d918bcSEd Tanous             {boost::beast::http::verb::patch, {{"ConfigureManager"}}},
1792e0d918bcSEd Tanous             {boost::beast::http::verb::put, {{"ConfigureManager"}}},
1793e0d918bcSEd Tanous             {boost::beast::http::verb::delete_, {{"ConfigureManager"}}},
1794e0d918bcSEd Tanous             {boost::beast::http::verb::post, {{"ConfigureManager"}}}};
17959c310685SBorawski.Lukasz     }
17969c310685SBorawski.Lukasz 
17979c310685SBorawski.Lukasz   private:
179855c7b7a2SEd Tanous     void doGet(crow::Response& res, const crow::Request& req,
17991abe55efSEd Tanous                const std::vector<std::string>& params) override
18001abe55efSEd Tanous     {
180183ff9ab6SJames Feist         // Collections don't include the static data added by SubRoute
180283ff9ab6SJames Feist         // because it has a duplicate entry for members
180355c7b7a2SEd Tanous         res.jsonValue["@odata.id"] = "/redfish/v1/Managers";
180455c7b7a2SEd Tanous         res.jsonValue["@odata.type"] = "#ManagerCollection.ManagerCollection";
180555c7b7a2SEd Tanous         res.jsonValue["@odata.context"] =
180655c7b7a2SEd Tanous             "/redfish/v1/$metadata#ManagerCollection.ManagerCollection";
180755c7b7a2SEd Tanous         res.jsonValue["Name"] = "Manager Collection";
180855c7b7a2SEd Tanous         res.jsonValue["Members@odata.count"] = 1;
180955c7b7a2SEd Tanous         res.jsonValue["Members"] = {
18105b4aa86bSJames Feist             {{"@odata.id", "/redfish/v1/Managers/bmc"}}};
18119c310685SBorawski.Lukasz         res.end();
18129c310685SBorawski.Lukasz     }
18139c310685SBorawski.Lukasz };
18149c310685SBorawski.Lukasz } // namespace redfish
1815