xref: /openbmc/bmcweb/features/redfish/lib/managers.hpp (revision b943aaef12ecfa575f15d2066dd85a4e492c8a59)
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/" +
281b7a08d04SJames Feist                             std::string(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/" +
3105b4aa86bSJames Feist                                 std::string(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/" +
3235b4aa86bSJames Feist                                 std::string(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;
480*b943aaefSJames Feist                             }
481*b943aaefSJames Feist                             else if (propertyPair.first == "SetPointOffset")
482*b943aaefSJames Feist                             {
483*b943aaefSJames Feist                                 const std::string* ptr =
484*b943aaefSJames Feist                                     std::get_if<std::string>(
485*b943aaefSJames Feist                                         &propertyPair.second);
486*b943aaefSJames Feist 
487*b943aaefSJames Feist                                 if (ptr == nullptr)
488*b943aaefSJames Feist                                 {
489*b943aaefSJames Feist                                     BMCWEB_LOG_ERROR << "Field Illegal "
490*b943aaefSJames Feist                                                      << propertyPair.first;
491*b943aaefSJames Feist                                     messages::internalError(asyncResp->res);
492*b943aaefSJames Feist                                     return;
493*b943aaefSJames Feist                                 }
494*b943aaefSJames Feist                                 // translate from dbus to redfish
495*b943aaefSJames Feist                                 if (*ptr == "WarningHigh")
496*b943aaefSJames Feist                                 {
497*b943aaefSJames Feist                                     (*config)["SetPointOffset"] =
498*b943aaefSJames Feist                                         "UpperThresholdNonCritical";
499*b943aaefSJames Feist                                 }
500*b943aaefSJames Feist                                 else if (*ptr == "WarningLow")
501*b943aaefSJames Feist                                 {
502*b943aaefSJames Feist                                     (*config)["SetPointOffset"] =
503*b943aaefSJames Feist                                         "LowerThresholdNonCritical";
504*b943aaefSJames Feist                                 }
505*b943aaefSJames Feist                                 else if (*ptr == "CriticalHigh")
506*b943aaefSJames Feist                                 {
507*b943aaefSJames Feist                                     (*config)["SetPointOffset"] =
508*b943aaefSJames Feist                                         "UpperThresholdCritical";
509*b943aaefSJames Feist                                 }
510*b943aaefSJames Feist                                 else if (*ptr == "CriticalLow")
511*b943aaefSJames Feist                                 {
512*b943aaefSJames Feist                                     (*config)["SetPointOffset"] =
513*b943aaefSJames Feist                                         "LowerThresholdCritical";
514*b943aaefSJames Feist                                 }
515*b943aaefSJames Feist                                 else
516*b943aaefSJames Feist                                 {
517*b943aaefSJames Feist                                     BMCWEB_LOG_ERROR << "Value Illegal "
518*b943aaefSJames Feist                                                      << *ptr;
519*b943aaefSJames Feist                                     messages::internalError(asyncResp->res);
520*b943aaefSJames Feist                                     return;
521*b943aaefSJames Feist                                 }
522*b943aaefSJames Feist                             }
523*b943aaefSJames 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         }
6695f2caaefSJames Feist         // delete interface
6705f2caaefSJames Feist         crow::connections::systemBus->async_method_call(
6715f2caaefSJames Feist             [response, path](const boost::system::error_code ec) {
6725f2caaefSJames Feist                 if (ec)
6735f2caaefSJames Feist                 {
6745f2caaefSJames Feist                     BMCWEB_LOG_ERROR << "Error patching " << path << ": " << ec;
6755f2caaefSJames Feist                     messages::internalError(response->res);
676b6baeaa4SJames Feist                     return;
6775f2caaefSJames Feist                 }
678b6baeaa4SJames Feist                 messages::success(response->res);
6795f2caaefSJames Feist             },
6805f2caaefSJames Feist             "xyz.openbmc_project.EntityManager", path, iface, "Delete");
6815f2caaefSJames Feist         return CreatePIDRet::del;
6825f2caaefSJames Feist     }
6835f2caaefSJames Feist 
68473df0db0SJames Feist     const dbus::utility::ManagedItem* managedItem = nullptr;
685b6baeaa4SJames Feist     if (!createNewObject)
686b6baeaa4SJames Feist     {
687b6baeaa4SJames Feist         // if we aren't creating a new object, we should be able to find it on
688b6baeaa4SJames Feist         // d-bus
68973df0db0SJames Feist         managedItem = findChassis(managedObj, it.key(), chassis);
69073df0db0SJames Feist         if (managedItem == nullptr)
691b6baeaa4SJames Feist         {
692b6baeaa4SJames Feist             BMCWEB_LOG_ERROR << "Failed to get chassis from config patch";
693b6baeaa4SJames Feist             messages::invalidObject(response->res, it.key());
694b6baeaa4SJames Feist             return CreatePIDRet::fail;
695b6baeaa4SJames Feist         }
696b6baeaa4SJames Feist     }
697b6baeaa4SJames Feist 
69873df0db0SJames Feist     if (profile.size() &&
69973df0db0SJames Feist         (type == "PidControllers" || type == "FanControllers" ||
70073df0db0SJames Feist          type == "StepwiseControllers"))
70173df0db0SJames Feist     {
70273df0db0SJames Feist         if (managedItem == nullptr)
70373df0db0SJames Feist         {
70473df0db0SJames Feist             output["Profiles"] = std::vector<std::string>{profile};
70573df0db0SJames Feist         }
70673df0db0SJames Feist         else
70773df0db0SJames Feist         {
70873df0db0SJames Feist             std::string interface;
70973df0db0SJames Feist             if (type == "StepwiseControllers")
71073df0db0SJames Feist             {
71173df0db0SJames Feist                 interface = stepwiseConfigurationIface;
71273df0db0SJames Feist             }
71373df0db0SJames Feist             else
71473df0db0SJames Feist             {
71573df0db0SJames Feist                 interface = pidConfigurationIface;
71673df0db0SJames Feist             }
71773df0db0SJames Feist             auto findConfig = managedItem->second.find(interface);
71873df0db0SJames Feist             if (findConfig == managedItem->second.end())
71973df0db0SJames Feist             {
72073df0db0SJames Feist                 BMCWEB_LOG_ERROR
72173df0db0SJames Feist                     << "Failed to find interface in managed object";
72273df0db0SJames Feist                 messages::internalError(response->res);
72373df0db0SJames Feist                 return CreatePIDRet::fail;
72473df0db0SJames Feist             }
72573df0db0SJames Feist             auto findProfiles = findConfig->second.find("Profiles");
72673df0db0SJames Feist             if (findProfiles != findConfig->second.end())
72773df0db0SJames Feist             {
72873df0db0SJames Feist                 const std::vector<std::string>* curProfiles =
72973df0db0SJames Feist                     std::get_if<std::vector<std::string>>(
73073df0db0SJames Feist                         &(findProfiles->second));
73173df0db0SJames Feist                 if (curProfiles == nullptr)
73273df0db0SJames Feist                 {
73373df0db0SJames Feist                     BMCWEB_LOG_ERROR << "Illegal profiles in managed object";
73473df0db0SJames Feist                     messages::internalError(response->res);
73573df0db0SJames Feist                     return CreatePIDRet::fail;
73673df0db0SJames Feist                 }
73773df0db0SJames Feist                 if (std::find(curProfiles->begin(), curProfiles->end(),
73873df0db0SJames Feist                               profile) == curProfiles->end())
73973df0db0SJames Feist                 {
74073df0db0SJames Feist                     std::vector<std::string> newProfiles = *curProfiles;
74173df0db0SJames Feist                     newProfiles.push_back(profile);
74273df0db0SJames Feist                     output["Profiles"] = newProfiles;
74373df0db0SJames Feist                 }
74473df0db0SJames Feist             }
74573df0db0SJames Feist         }
74673df0db0SJames Feist     }
74773df0db0SJames Feist 
74883ff9ab6SJames Feist     if (type == "PidControllers" || type == "FanControllers")
74983ff9ab6SJames Feist     {
75083ff9ab6SJames Feist         if (createNewObject)
75183ff9ab6SJames Feist         {
75283ff9ab6SJames Feist             output["Class"] = type == "PidControllers" ? std::string("temp")
75383ff9ab6SJames Feist                                                        : std::string("fan");
75483ff9ab6SJames Feist             output["Type"] = std::string("Pid");
75583ff9ab6SJames Feist         }
7565f2caaefSJames Feist 
7575f2caaefSJames Feist         std::optional<std::vector<nlohmann::json>> zones;
7585f2caaefSJames Feist         std::optional<std::vector<std::string>> inputs;
7595f2caaefSJames Feist         std::optional<std::vector<std::string>> outputs;
7605f2caaefSJames Feist         std::map<std::string, std::optional<double>> doubles;
761*b943aaefSJames Feist         std::optional<std::string> setpointOffset;
7625f2caaefSJames Feist         if (!redfish::json_util::readJson(
763b6baeaa4SJames Feist                 it.value(), response->res, "Inputs", inputs, "Outputs", outputs,
7645f2caaefSJames Feist                 "Zones", zones, "FFGainCoefficient",
7655f2caaefSJames Feist                 doubles["FFGainCoefficient"], "FFOffCoefficient",
7665f2caaefSJames Feist                 doubles["FFOffCoefficient"], "ICoefficient",
7675f2caaefSJames Feist                 doubles["ICoefficient"], "ILimitMax", doubles["ILimitMax"],
7685f2caaefSJames Feist                 "ILimitMin", doubles["ILimitMin"], "OutLimitMax",
7695f2caaefSJames Feist                 doubles["OutLimitMax"], "OutLimitMin", doubles["OutLimitMin"],
7705f2caaefSJames Feist                 "PCoefficient", doubles["PCoefficient"], "SetPoint",
771*b943aaefSJames Feist                 doubles["SetPoint"], "SetPointOffset", setpointOffset,
772*b943aaefSJames Feist                 "SlewNeg", doubles["SlewNeg"], "SlewPos", doubles["SlewPos"],
773*b943aaefSJames Feist                 "PositiveHysteresis", doubles["PositiveHysteresis"],
774*b943aaefSJames Feist                 "NegativeHysteresis", doubles["NegativeHysteresis"]))
77583ff9ab6SJames Feist         {
7765f2caaefSJames Feist             BMCWEB_LOG_ERROR << "Line:" << __LINE__ << ", Illegal Property "
777b6baeaa4SJames Feist                              << it.value().dump();
7785f2caaefSJames Feist             return CreatePIDRet::fail;
77983ff9ab6SJames Feist         }
7805f2caaefSJames Feist         if (zones)
7815f2caaefSJames Feist         {
7825f2caaefSJames Feist             std::vector<std::string> zonesStr;
7835f2caaefSJames Feist             if (!getZonesFromJsonReq(response, *zones, zonesStr))
7845f2caaefSJames Feist             {
7855f2caaefSJames Feist                 BMCWEB_LOG_ERROR << "Line:" << __LINE__ << ", Illegal Zones";
7865f2caaefSJames Feist                 return CreatePIDRet::fail;
7875f2caaefSJames Feist             }
788b6baeaa4SJames Feist             if (chassis.empty() &&
789b6baeaa4SJames Feist                 !findChassis(managedObj, zonesStr[0], chassis))
790b6baeaa4SJames Feist             {
791b6baeaa4SJames Feist                 BMCWEB_LOG_ERROR << "Failed to get chassis from config patch";
792b6baeaa4SJames Feist                 messages::invalidObject(response->res, it.key());
793b6baeaa4SJames Feist                 return CreatePIDRet::fail;
794b6baeaa4SJames Feist             }
795b6baeaa4SJames Feist 
7965f2caaefSJames Feist             output["Zones"] = std::move(zonesStr);
7975f2caaefSJames Feist         }
7985f2caaefSJames Feist         if (inputs || outputs)
7995f2caaefSJames Feist         {
8005f2caaefSJames Feist             std::array<std::optional<std::vector<std::string>>*, 2> containers =
8015f2caaefSJames Feist                 {&inputs, &outputs};
8025f2caaefSJames Feist             size_t index = 0;
8035f2caaefSJames Feist             for (const auto& containerPtr : containers)
8045f2caaefSJames Feist             {
8055f2caaefSJames Feist                 std::optional<std::vector<std::string>>& container =
8065f2caaefSJames Feist                     *containerPtr;
8075f2caaefSJames Feist                 if (!container)
8085f2caaefSJames Feist                 {
8095f2caaefSJames Feist                     index++;
8105f2caaefSJames Feist                     continue;
81183ff9ab6SJames Feist                 }
81283ff9ab6SJames Feist 
8135f2caaefSJames Feist                 for (std::string& value : *container)
81483ff9ab6SJames Feist                 {
8155f2caaefSJames Feist                     boost::replace_all(value, "_", " ");
81683ff9ab6SJames Feist                 }
8175f2caaefSJames Feist                 std::string key;
8185f2caaefSJames Feist                 if (index == 0)
8195f2caaefSJames Feist                 {
8205f2caaefSJames Feist                     key = "Inputs";
8215f2caaefSJames Feist                 }
8225f2caaefSJames Feist                 else
8235f2caaefSJames Feist                 {
8245f2caaefSJames Feist                     key = "Outputs";
8255f2caaefSJames Feist                 }
8265f2caaefSJames Feist                 output[key] = *container;
8275f2caaefSJames Feist                 index++;
8285f2caaefSJames Feist             }
82983ff9ab6SJames Feist         }
83083ff9ab6SJames Feist 
831*b943aaefSJames Feist         if (setpointOffset)
832*b943aaefSJames Feist         {
833*b943aaefSJames Feist             // translate between redfish and dbus names
834*b943aaefSJames Feist             if (*setpointOffset == "UpperThresholdNonCritical")
835*b943aaefSJames Feist             {
836*b943aaefSJames Feist                 output["SetPointOffset"] = std::string("WarningLow");
837*b943aaefSJames Feist             }
838*b943aaefSJames Feist             else if (*setpointOffset == "LowerThresholdNonCritical")
839*b943aaefSJames Feist             {
840*b943aaefSJames Feist                 output["SetPointOffset"] = std::string("WarningHigh");
841*b943aaefSJames Feist             }
842*b943aaefSJames Feist             else if (*setpointOffset == "LowerThresholdCritical")
843*b943aaefSJames Feist             {
844*b943aaefSJames Feist                 output["SetPointOffset"] = std::string("CriticalLow");
845*b943aaefSJames Feist             }
846*b943aaefSJames Feist             else if (*setpointOffset == "UpperThresholdCritical")
847*b943aaefSJames Feist             {
848*b943aaefSJames Feist                 output["SetPointOffset"] = std::string("CriticalHigh");
849*b943aaefSJames Feist             }
850*b943aaefSJames Feist             else
851*b943aaefSJames Feist             {
852*b943aaefSJames Feist                 BMCWEB_LOG_ERROR << "Invalid setpointoffset "
853*b943aaefSJames Feist                                  << *setpointOffset;
854*b943aaefSJames Feist                 messages::invalidObject(response->res, it.key());
855*b943aaefSJames Feist                 return CreatePIDRet::fail;
856*b943aaefSJames Feist             }
857*b943aaefSJames Feist         }
858*b943aaefSJames Feist 
85983ff9ab6SJames Feist         // doubles
8605f2caaefSJames Feist         for (const auto& pairs : doubles)
86183ff9ab6SJames Feist         {
8625f2caaefSJames Feist             if (!pairs.second)
86383ff9ab6SJames Feist             {
8645f2caaefSJames Feist                 continue;
86583ff9ab6SJames Feist             }
8665f2caaefSJames Feist             BMCWEB_LOG_DEBUG << pairs.first << " = " << *pairs.second;
8675f2caaefSJames Feist             output[pairs.first] = *(pairs.second);
8685f2caaefSJames Feist         }
86983ff9ab6SJames Feist     }
87083ff9ab6SJames Feist 
87183ff9ab6SJames Feist     else if (type == "FanZones")
87283ff9ab6SJames Feist     {
87383ff9ab6SJames Feist         output["Type"] = std::string("Pid.Zone");
87483ff9ab6SJames Feist 
8755f2caaefSJames Feist         std::optional<nlohmann::json> chassisContainer;
8765f2caaefSJames Feist         std::optional<double> failSafePercent;
877d3ec07f8SJames Feist         std::optional<double> minThermalOutput;
878b6baeaa4SJames Feist         if (!redfish::json_util::readJson(it.value(), response->res, "Chassis",
8795f2caaefSJames Feist                                           chassisContainer, "FailSafePercent",
880d3ec07f8SJames Feist                                           failSafePercent, "MinThermalOutput",
881d3ec07f8SJames Feist                                           minThermalOutput))
88283ff9ab6SJames Feist         {
8835f2caaefSJames Feist             BMCWEB_LOG_ERROR << "Line:" << __LINE__ << ", Illegal Property "
884b6baeaa4SJames Feist                              << it.value().dump();
88583ff9ab6SJames Feist             return CreatePIDRet::fail;
88683ff9ab6SJames Feist         }
8875f2caaefSJames Feist 
8885f2caaefSJames Feist         if (chassisContainer)
88983ff9ab6SJames Feist         {
8905f2caaefSJames Feist 
8915f2caaefSJames Feist             std::string chassisId;
8925f2caaefSJames Feist             if (!redfish::json_util::readJson(*chassisContainer, response->res,
8935f2caaefSJames Feist                                               "@odata.id", chassisId))
8945f2caaefSJames Feist             {
8955f2caaefSJames Feist                 BMCWEB_LOG_ERROR << "Line:" << __LINE__ << ", Illegal Property "
8965f2caaefSJames Feist                                  << chassisContainer->dump();
89783ff9ab6SJames Feist                 return CreatePIDRet::fail;
89883ff9ab6SJames Feist             }
89983ff9ab6SJames Feist 
90083ff9ab6SJames Feist             // /refish/v1/chassis/chassis_name/
9015f2caaefSJames Feist             if (!dbus::utility::getNthStringFromPath(chassisId, 3, chassis))
90283ff9ab6SJames Feist             {
9035f2caaefSJames Feist                 BMCWEB_LOG_ERROR << "Got invalid path " << chassisId;
9045f2caaefSJames Feist                 messages::invalidObject(response->res, chassisId);
90583ff9ab6SJames Feist                 return CreatePIDRet::fail;
90683ff9ab6SJames Feist             }
90783ff9ab6SJames Feist         }
908d3ec07f8SJames Feist         if (minThermalOutput)
90983ff9ab6SJames Feist         {
910d3ec07f8SJames Feist             output["MinThermalOutput"] = *minThermalOutput;
9115f2caaefSJames Feist         }
9125f2caaefSJames Feist         if (failSafePercent)
91383ff9ab6SJames Feist         {
9145f2caaefSJames Feist             output["FailSafePercent"] = *failSafePercent;
9155f2caaefSJames Feist         }
9165f2caaefSJames Feist     }
9175f2caaefSJames Feist     else if (type == "StepwiseControllers")
9185f2caaefSJames Feist     {
9195f2caaefSJames Feist         output["Type"] = std::string("Stepwise");
9205f2caaefSJames Feist 
9215f2caaefSJames Feist         std::optional<std::vector<nlohmann::json>> zones;
9225f2caaefSJames Feist         std::optional<std::vector<nlohmann::json>> steps;
9235f2caaefSJames Feist         std::optional<std::vector<std::string>> inputs;
9245f2caaefSJames Feist         std::optional<double> positiveHysteresis;
9255f2caaefSJames Feist         std::optional<double> negativeHysteresis;
926c33a90ecSJames Feist         std::optional<std::string> direction; // upper clipping curve vs lower
9275f2caaefSJames Feist         if (!redfish::json_util::readJson(
928b6baeaa4SJames Feist                 it.value(), response->res, "Zones", zones, "Steps", steps,
929b6baeaa4SJames Feist                 "Inputs", inputs, "PositiveHysteresis", positiveHysteresis,
930c33a90ecSJames Feist                 "NegativeHysteresis", negativeHysteresis, "Direction",
931c33a90ecSJames Feist                 direction))
9325f2caaefSJames Feist         {
9335f2caaefSJames Feist             BMCWEB_LOG_ERROR << "Line:" << __LINE__ << ", Illegal Property "
934b6baeaa4SJames Feist                              << it.value().dump();
93583ff9ab6SJames Feist             return CreatePIDRet::fail;
93683ff9ab6SJames Feist         }
9375f2caaefSJames Feist 
9385f2caaefSJames Feist         if (zones)
93983ff9ab6SJames Feist         {
940b6baeaa4SJames Feist             std::vector<std::string> zonesStrs;
941b6baeaa4SJames Feist             if (!getZonesFromJsonReq(response, *zones, zonesStrs))
9425f2caaefSJames Feist             {
9435f2caaefSJames Feist                 BMCWEB_LOG_ERROR << "Line:" << __LINE__ << ", Illegal Zones";
94483ff9ab6SJames Feist                 return CreatePIDRet::fail;
94583ff9ab6SJames Feist             }
946b6baeaa4SJames Feist             if (chassis.empty() &&
947b6baeaa4SJames Feist                 !findChassis(managedObj, zonesStrs[0], chassis))
948b6baeaa4SJames Feist             {
949b6baeaa4SJames Feist                 BMCWEB_LOG_ERROR << "Failed to get chassis from config patch";
950b6baeaa4SJames Feist                 messages::invalidObject(response->res, it.key());
951b6baeaa4SJames Feist                 return CreatePIDRet::fail;
952b6baeaa4SJames Feist             }
953b6baeaa4SJames Feist             output["Zones"] = std::move(zonesStrs);
9545f2caaefSJames Feist         }
9555f2caaefSJames Feist         if (steps)
9565f2caaefSJames Feist         {
9575f2caaefSJames Feist             std::vector<double> readings;
9585f2caaefSJames Feist             std::vector<double> outputs;
9595f2caaefSJames Feist             for (auto& step : *steps)
9605f2caaefSJames Feist             {
9615f2caaefSJames Feist                 double target;
962b01bf299SEd Tanous                 double output;
9635f2caaefSJames Feist 
9645f2caaefSJames Feist                 if (!redfish::json_util::readJson(step, response->res, "Target",
965b01bf299SEd Tanous                                                   target, "Output", output))
9665f2caaefSJames Feist                 {
9675f2caaefSJames Feist                     BMCWEB_LOG_ERROR << "Line:" << __LINE__
968b6baeaa4SJames Feist                                      << ", Illegal Property "
969b6baeaa4SJames Feist                                      << it.value().dump();
9705f2caaefSJames Feist                     return CreatePIDRet::fail;
9715f2caaefSJames Feist                 }
9725f2caaefSJames Feist                 readings.emplace_back(target);
973b01bf299SEd Tanous                 outputs.emplace_back(output);
9745f2caaefSJames Feist             }
9755f2caaefSJames Feist             output["Reading"] = std::move(readings);
9765f2caaefSJames Feist             output["Output"] = std::move(outputs);
9775f2caaefSJames Feist         }
9785f2caaefSJames Feist         if (inputs)
9795f2caaefSJames Feist         {
9805f2caaefSJames Feist             for (std::string& value : *inputs)
9815f2caaefSJames Feist             {
9825f2caaefSJames Feist                 boost::replace_all(value, "_", " ");
9835f2caaefSJames Feist             }
9845f2caaefSJames Feist             output["Inputs"] = std::move(*inputs);
9855f2caaefSJames Feist         }
9865f2caaefSJames Feist         if (negativeHysteresis)
9875f2caaefSJames Feist         {
9885f2caaefSJames Feist             output["NegativeHysteresis"] = *negativeHysteresis;
9895f2caaefSJames Feist         }
9905f2caaefSJames Feist         if (positiveHysteresis)
9915f2caaefSJames Feist         {
9925f2caaefSJames Feist             output["PositiveHysteresis"] = *positiveHysteresis;
99383ff9ab6SJames Feist         }
994c33a90ecSJames Feist         if (direction)
995c33a90ecSJames Feist         {
996c33a90ecSJames Feist             constexpr const std::array<const char*, 2> allowedDirections = {
997c33a90ecSJames Feist                 "Ceiling", "Floor"};
998c33a90ecSJames Feist             if (std::find(allowedDirections.begin(), allowedDirections.end(),
999c33a90ecSJames Feist                           *direction) == allowedDirections.end())
1000c33a90ecSJames Feist             {
1001c33a90ecSJames Feist                 messages::propertyValueTypeError(response->res, "Direction",
1002c33a90ecSJames Feist                                                  *direction);
1003c33a90ecSJames Feist                 return CreatePIDRet::fail;
1004c33a90ecSJames Feist             }
1005c33a90ecSJames Feist             output["Class"] = *direction;
1006c33a90ecSJames Feist         }
100783ff9ab6SJames Feist     }
100883ff9ab6SJames Feist     else
100983ff9ab6SJames Feist     {
10105f2caaefSJames Feist         BMCWEB_LOG_ERROR << "Line:" << __LINE__ << ", Illegal Type " << type;
101135a62c7cSJason M. Bills         messages::propertyUnknown(response->res, type);
101283ff9ab6SJames Feist         return CreatePIDRet::fail;
101383ff9ab6SJames Feist     }
101483ff9ab6SJames Feist     return CreatePIDRet::patch;
101583ff9ab6SJames Feist }
101673df0db0SJames Feist struct GetPIDValues : std::enable_shared_from_this<GetPIDValues>
101773df0db0SJames Feist {
101883ff9ab6SJames Feist 
101973df0db0SJames Feist     GetPIDValues(const std::shared_ptr<AsyncResp>& asyncResp) :
102073df0db0SJames Feist         asyncResp(asyncResp)
102173df0db0SJames Feist 
10221abe55efSEd Tanous     {
10239c310685SBorawski.Lukasz     }
10249c310685SBorawski.Lukasz 
102573df0db0SJames Feist     void run()
10265b4aa86bSJames Feist     {
102773df0db0SJames Feist         std::shared_ptr<GetPIDValues> self = shared_from_this();
102873df0db0SJames Feist 
102973df0db0SJames Feist         // get all configurations
10305b4aa86bSJames Feist         crow::connections::systemBus->async_method_call(
103173df0db0SJames Feist             [self](const boost::system::error_code ec,
10325b4aa86bSJames Feist                    const crow::openbmc_mapper::GetSubTreeType& subtree) {
10335b4aa86bSJames Feist                 if (ec)
10345b4aa86bSJames Feist                 {
10355b4aa86bSJames Feist                     BMCWEB_LOG_ERROR << ec;
103673df0db0SJames Feist                     messages::internalError(self->asyncResp->res);
103773df0db0SJames Feist                     return;
103873df0db0SJames Feist                 }
103973df0db0SJames Feist                 self->subtree = subtree;
104073df0db0SJames Feist             },
104173df0db0SJames Feist             "xyz.openbmc_project.ObjectMapper",
104273df0db0SJames Feist             "/xyz/openbmc_project/object_mapper",
104373df0db0SJames Feist             "xyz.openbmc_project.ObjectMapper", "GetSubTree", "/", 0,
104473df0db0SJames Feist             std::array<const char*, 4>{
104573df0db0SJames Feist                 pidConfigurationIface, pidZoneConfigurationIface,
104673df0db0SJames Feist                 objectManagerIface, stepwiseConfigurationIface});
104773df0db0SJames Feist 
104873df0db0SJames Feist         // at the same time get the selected profile
104973df0db0SJames Feist         crow::connections::systemBus->async_method_call(
105073df0db0SJames Feist             [self](const boost::system::error_code ec,
105173df0db0SJames Feist                    const crow::openbmc_mapper::GetSubTreeType& subtree) {
105273df0db0SJames Feist                 if (ec || subtree.empty())
105373df0db0SJames Feist                 {
105473df0db0SJames Feist                     return;
105573df0db0SJames Feist                 }
105673df0db0SJames Feist                 if (subtree[0].second.size() != 1)
105773df0db0SJames Feist                 {
105873df0db0SJames Feist                     // invalid mapper response, should never happen
105973df0db0SJames Feist                     BMCWEB_LOG_ERROR << "GetPIDValues: Mapper Error";
106073df0db0SJames Feist                     messages::internalError(self->asyncResp->res);
10615b4aa86bSJames Feist                     return;
10625b4aa86bSJames Feist                 }
10635b4aa86bSJames Feist 
106473df0db0SJames Feist                 const std::string& path = subtree[0].first;
106573df0db0SJames Feist                 const std::string& owner = subtree[0].second[0].first;
106673df0db0SJames Feist                 crow::connections::systemBus->async_method_call(
106773df0db0SJames Feist                     [path, owner, self](
106873df0db0SJames Feist                         const boost::system::error_code ec,
106973df0db0SJames Feist                         const boost::container::flat_map<
107073df0db0SJames Feist                             std::string, std::variant<std::vector<std::string>,
107173df0db0SJames Feist                                                       std::string>>& resp) {
107273df0db0SJames Feist                         if (ec)
107373df0db0SJames Feist                         {
107473df0db0SJames Feist                             BMCWEB_LOG_ERROR << "GetPIDValues: Can't get "
107573df0db0SJames Feist                                                 "thermalModeIface "
107673df0db0SJames Feist                                              << path;
107773df0db0SJames Feist                             messages::internalError(self->asyncResp->res);
107873df0db0SJames Feist                             return;
107973df0db0SJames Feist                         }
108073df0db0SJames Feist                         const std::string* current;
108173df0db0SJames Feist                         const std::vector<std::string>* supported;
108273df0db0SJames Feist                         for (auto& [key, value] : resp)
108373df0db0SJames Feist                         {
108473df0db0SJames Feist                             if (key == "Current")
108573df0db0SJames Feist                             {
108673df0db0SJames Feist                                 current = std::get_if<std::string>(&value);
108773df0db0SJames Feist                                 if (current == nullptr)
108873df0db0SJames Feist                                 {
108973df0db0SJames Feist                                     BMCWEB_LOG_ERROR
109073df0db0SJames Feist                                         << "GetPIDValues: thermal mode "
109173df0db0SJames Feist                                            "iface invalid "
109273df0db0SJames Feist                                         << path;
109373df0db0SJames Feist                                     messages::internalError(
109473df0db0SJames Feist                                         self->asyncResp->res);
109573df0db0SJames Feist                                     return;
109673df0db0SJames Feist                                 }
109773df0db0SJames Feist                             }
109873df0db0SJames Feist                             if (key == "Supported")
109973df0db0SJames Feist                             {
110073df0db0SJames Feist                                 supported =
110173df0db0SJames Feist                                     std::get_if<std::vector<std::string>>(
110273df0db0SJames Feist                                         &value);
110373df0db0SJames Feist                                 if (supported == nullptr)
110473df0db0SJames Feist                                 {
110573df0db0SJames Feist                                     BMCWEB_LOG_ERROR
110673df0db0SJames Feist                                         << "GetPIDValues: thermal mode "
110773df0db0SJames Feist                                            "iface invalid"
110873df0db0SJames Feist                                         << path;
110973df0db0SJames Feist                                     messages::internalError(
111073df0db0SJames Feist                                         self->asyncResp->res);
111173df0db0SJames Feist                                     return;
111273df0db0SJames Feist                                 }
111373df0db0SJames Feist                             }
111473df0db0SJames Feist                         }
111573df0db0SJames Feist                         if (current == nullptr || supported == nullptr)
111673df0db0SJames Feist                         {
111773df0db0SJames Feist                             BMCWEB_LOG_ERROR << "GetPIDValues: thermal mode "
111873df0db0SJames Feist                                                 "iface invalid "
111973df0db0SJames Feist                                              << path;
112073df0db0SJames Feist                             messages::internalError(self->asyncResp->res);
112173df0db0SJames Feist                             return;
112273df0db0SJames Feist                         }
112373df0db0SJames Feist                         self->currentProfile = *current;
112473df0db0SJames Feist                         self->supportedProfiles = *supported;
112573df0db0SJames Feist                     },
112673df0db0SJames Feist                     owner, path, "org.freedesktop.DBus.Properties", "GetAll",
112773df0db0SJames Feist                     thermalModeIface);
112873df0db0SJames Feist             },
112973df0db0SJames Feist             "xyz.openbmc_project.ObjectMapper",
113073df0db0SJames Feist             "/xyz/openbmc_project/object_mapper",
113173df0db0SJames Feist             "xyz.openbmc_project.ObjectMapper", "GetSubTree", "/", 0,
113273df0db0SJames Feist             std::array<const char*, 1>{thermalModeIface});
113373df0db0SJames Feist     }
113473df0db0SJames Feist 
113573df0db0SJames Feist     ~GetPIDValues()
113673df0db0SJames Feist     {
113773df0db0SJames Feist         if (asyncResp->res.result() != boost::beast::http::status::ok)
113873df0db0SJames Feist         {
113973df0db0SJames Feist             return;
114073df0db0SJames Feist         }
11415b4aa86bSJames Feist         // create map of <connection, path to objMgr>>
114273df0db0SJames Feist         boost::container::flat_map<std::string, std::string> objectMgrPaths;
11436bce33bcSJames Feist         boost::container::flat_set<std::string> calledConnections;
11445b4aa86bSJames Feist         for (const auto& pathGroup : subtree)
11455b4aa86bSJames Feist         {
11465b4aa86bSJames Feist             for (const auto& connectionGroup : pathGroup.second)
11475b4aa86bSJames Feist             {
11486bce33bcSJames Feist                 auto findConnection =
11496bce33bcSJames Feist                     calledConnections.find(connectionGroup.first);
11506bce33bcSJames Feist                 if (findConnection != calledConnections.end())
11516bce33bcSJames Feist                 {
11526bce33bcSJames Feist                     break;
11536bce33bcSJames Feist                 }
115473df0db0SJames Feist                 for (const std::string& interface : connectionGroup.second)
11555b4aa86bSJames Feist                 {
11565b4aa86bSJames Feist                     if (interface == objectManagerIface)
11575b4aa86bSJames Feist                     {
115873df0db0SJames Feist                         objectMgrPaths[connectionGroup.first] = pathGroup.first;
11595b4aa86bSJames Feist                     }
11605b4aa86bSJames Feist                     // this list is alphabetical, so we
11615b4aa86bSJames Feist                     // should have found the objMgr by now
11625b4aa86bSJames Feist                     if (interface == pidConfigurationIface ||
1163b7a08d04SJames Feist                         interface == pidZoneConfigurationIface ||
1164b7a08d04SJames Feist                         interface == stepwiseConfigurationIface)
11655b4aa86bSJames Feist                     {
11665b4aa86bSJames Feist                         auto findObjMgr =
11675b4aa86bSJames Feist                             objectMgrPaths.find(connectionGroup.first);
11685b4aa86bSJames Feist                         if (findObjMgr == objectMgrPaths.end())
11695b4aa86bSJames Feist                         {
11705b4aa86bSJames Feist                             BMCWEB_LOG_DEBUG << connectionGroup.first
11715b4aa86bSJames Feist                                              << "Has no Object Manager";
11725b4aa86bSJames Feist                             continue;
11735b4aa86bSJames Feist                         }
11746bce33bcSJames Feist 
11756bce33bcSJames Feist                         calledConnections.insert(connectionGroup.first);
11766bce33bcSJames Feist 
117773df0db0SJames Feist                         asyncPopulatePid(findObjMgr->first, findObjMgr->second,
117873df0db0SJames Feist                                          currentProfile, supportedProfiles,
117973df0db0SJames Feist                                          asyncResp);
11805b4aa86bSJames Feist                         break;
11815b4aa86bSJames Feist                     }
11825b4aa86bSJames Feist                 }
11835b4aa86bSJames Feist             }
11845b4aa86bSJames Feist         }
118573df0db0SJames Feist     }
118673df0db0SJames Feist 
118773df0db0SJames Feist     std::vector<std::string> supportedProfiles;
118873df0db0SJames Feist     std::string currentProfile;
118973df0db0SJames Feist     crow::openbmc_mapper::GetSubTreeType subtree;
119073df0db0SJames Feist     std::shared_ptr<AsyncResp> asyncResp;
119173df0db0SJames Feist };
119273df0db0SJames Feist 
119373df0db0SJames Feist struct SetPIDValues : std::enable_shared_from_this<SetPIDValues>
119473df0db0SJames Feist {
119573df0db0SJames Feist 
119673df0db0SJames Feist     SetPIDValues(const std::shared_ptr<AsyncResp>& asyncResp,
119773df0db0SJames Feist                  nlohmann::json& data) :
119873df0db0SJames Feist         asyncResp(asyncResp)
119973df0db0SJames Feist     {
120073df0db0SJames Feist 
120173df0db0SJames Feist         std::optional<nlohmann::json> pidControllers;
120273df0db0SJames Feist         std::optional<nlohmann::json> fanControllers;
120373df0db0SJames Feist         std::optional<nlohmann::json> fanZones;
120473df0db0SJames Feist         std::optional<nlohmann::json> stepwiseControllers;
120573df0db0SJames Feist 
120673df0db0SJames Feist         if (!redfish::json_util::readJson(
120773df0db0SJames Feist                 data, asyncResp->res, "PidControllers", pidControllers,
120873df0db0SJames Feist                 "FanControllers", fanControllers, "FanZones", fanZones,
120973df0db0SJames Feist                 "StepwiseControllers", stepwiseControllers, "Profile", profile))
121073df0db0SJames Feist         {
121173df0db0SJames Feist             BMCWEB_LOG_ERROR << "Line:" << __LINE__ << ", Illegal Property "
121273df0db0SJames Feist                              << data.dump();
121373df0db0SJames Feist             return;
121473df0db0SJames Feist         }
121573df0db0SJames Feist         configuration.emplace_back("PidControllers", std::move(pidControllers));
121673df0db0SJames Feist         configuration.emplace_back("FanControllers", std::move(fanControllers));
121773df0db0SJames Feist         configuration.emplace_back("FanZones", std::move(fanZones));
121873df0db0SJames Feist         configuration.emplace_back("StepwiseControllers",
121973df0db0SJames Feist                                    std::move(stepwiseControllers));
122073df0db0SJames Feist     }
122173df0db0SJames Feist     void run()
122273df0db0SJames Feist     {
122373df0db0SJames Feist         if (asyncResp->res.result() != boost::beast::http::status::ok)
122473df0db0SJames Feist         {
122573df0db0SJames Feist             return;
122673df0db0SJames Feist         }
122773df0db0SJames Feist 
122873df0db0SJames Feist         std::shared_ptr<SetPIDValues> self = shared_from_this();
122973df0db0SJames Feist 
123073df0db0SJames Feist         // todo(james): might make sense to do a mapper call here if this
123173df0db0SJames Feist         // interface gets more traction
123273df0db0SJames Feist         crow::connections::systemBus->async_method_call(
123373df0db0SJames Feist             [self](const boost::system::error_code ec,
123473df0db0SJames Feist                    dbus::utility::ManagedObjectType& managedObj) {
123573df0db0SJames Feist                 if (ec)
123673df0db0SJames Feist                 {
123773df0db0SJames Feist                     BMCWEB_LOG_ERROR << "Error communicating to Entity Manager";
123873df0db0SJames Feist                     messages::internalError(self->asyncResp->res);
123973df0db0SJames Feist                     return;
124073df0db0SJames Feist                 }
124173df0db0SJames Feist                 self->managedObj = std::move(managedObj);
124273df0db0SJames Feist             },
124373df0db0SJames Feist             "xyz.openbmc_project.EntityManager", "/", objectManagerIface,
124473df0db0SJames Feist             "GetManagedObjects");
124573df0db0SJames Feist 
124673df0db0SJames Feist         // at the same time get the profile information
124773df0db0SJames Feist         crow::connections::systemBus->async_method_call(
124873df0db0SJames Feist             [self](const boost::system::error_code ec,
124973df0db0SJames Feist                    const crow::openbmc_mapper::GetSubTreeType& subtree) {
125073df0db0SJames Feist                 if (ec || subtree.empty())
125173df0db0SJames Feist                 {
125273df0db0SJames Feist                     return;
125373df0db0SJames Feist                 }
125473df0db0SJames Feist                 if (subtree[0].second.empty())
125573df0db0SJames Feist                 {
125673df0db0SJames Feist                     // invalid mapper response, should never happen
125773df0db0SJames Feist                     BMCWEB_LOG_ERROR << "SetPIDValues: Mapper Error";
125873df0db0SJames Feist                     messages::internalError(self->asyncResp->res);
125973df0db0SJames Feist                     return;
126073df0db0SJames Feist                 }
126173df0db0SJames Feist 
126273df0db0SJames Feist                 const std::string& path = subtree[0].first;
126373df0db0SJames Feist                 const std::string& owner = subtree[0].second[0].first;
126473df0db0SJames Feist                 crow::connections::systemBus->async_method_call(
126573df0db0SJames Feist                     [self, path, owner](
126673df0db0SJames Feist                         const boost::system::error_code ec,
126773df0db0SJames Feist                         const boost::container::flat_map<
126873df0db0SJames Feist                             std::string, std::variant<std::vector<std::string>,
126973df0db0SJames Feist                                                       std::string>>& resp) {
127073df0db0SJames Feist                         if (ec)
127173df0db0SJames Feist                         {
127273df0db0SJames Feist                             BMCWEB_LOG_ERROR << "SetPIDValues: Can't get "
127373df0db0SJames Feist                                                 "thermalModeIface "
127473df0db0SJames Feist                                              << path;
127573df0db0SJames Feist                             messages::internalError(self->asyncResp->res);
127673df0db0SJames Feist                             return;
127773df0db0SJames Feist                         }
127873df0db0SJames Feist                         const std::string* current;
127973df0db0SJames Feist                         const std::vector<std::string>* supported;
128073df0db0SJames Feist                         for (auto& [key, value] : resp)
128173df0db0SJames Feist                         {
128273df0db0SJames Feist                             if (key == "Current")
128373df0db0SJames Feist                             {
128473df0db0SJames Feist                                 current = std::get_if<std::string>(&value);
128573df0db0SJames Feist                                 if (current == nullptr)
128673df0db0SJames Feist                                 {
128773df0db0SJames Feist                                     BMCWEB_LOG_ERROR
128873df0db0SJames Feist                                         << "SetPIDValues: thermal mode "
128973df0db0SJames Feist                                            "iface invalid "
129073df0db0SJames Feist                                         << path;
129173df0db0SJames Feist                                     messages::internalError(
129273df0db0SJames Feist                                         self->asyncResp->res);
129373df0db0SJames Feist                                     return;
129473df0db0SJames Feist                                 }
129573df0db0SJames Feist                             }
129673df0db0SJames Feist                             if (key == "Supported")
129773df0db0SJames Feist                             {
129873df0db0SJames Feist                                 supported =
129973df0db0SJames Feist                                     std::get_if<std::vector<std::string>>(
130073df0db0SJames Feist                                         &value);
130173df0db0SJames Feist                                 if (supported == nullptr)
130273df0db0SJames Feist                                 {
130373df0db0SJames Feist                                     BMCWEB_LOG_ERROR
130473df0db0SJames Feist                                         << "SetPIDValues: thermal mode "
130573df0db0SJames Feist                                            "iface invalid"
130673df0db0SJames Feist                                         << path;
130773df0db0SJames Feist                                     messages::internalError(
130873df0db0SJames Feist                                         self->asyncResp->res);
130973df0db0SJames Feist                                     return;
131073df0db0SJames Feist                                 }
131173df0db0SJames Feist                             }
131273df0db0SJames Feist                         }
131373df0db0SJames Feist                         if (current == nullptr || supported == nullptr)
131473df0db0SJames Feist                         {
131573df0db0SJames Feist                             BMCWEB_LOG_ERROR << "SetPIDValues: thermal mode "
131673df0db0SJames Feist                                                 "iface invalid "
131773df0db0SJames Feist                                              << path;
131873df0db0SJames Feist                             messages::internalError(self->asyncResp->res);
131973df0db0SJames Feist                             return;
132073df0db0SJames Feist                         }
132173df0db0SJames Feist                         self->currentProfile = *current;
132273df0db0SJames Feist                         self->supportedProfiles = *supported;
132373df0db0SJames Feist                         self->profileConnection = owner;
132473df0db0SJames Feist                         self->profilePath = path;
132573df0db0SJames Feist                     },
132673df0db0SJames Feist                     owner, path, "org.freedesktop.DBus.Properties", "GetAll",
132773df0db0SJames Feist                     thermalModeIface);
13285b4aa86bSJames Feist             },
13295b4aa86bSJames Feist             "xyz.openbmc_project.ObjectMapper",
13305b4aa86bSJames Feist             "/xyz/openbmc_project/object_mapper",
13315b4aa86bSJames Feist             "xyz.openbmc_project.ObjectMapper", "GetSubTree", "/", 0,
133273df0db0SJames Feist             std::array<const char*, 1>{thermalModeIface});
133373df0db0SJames Feist     }
133473df0db0SJames Feist     ~SetPIDValues()
133573df0db0SJames Feist     {
133673df0db0SJames Feist         if (asyncResp->res.result() != boost::beast::http::status::ok)
133773df0db0SJames Feist         {
133873df0db0SJames Feist             return;
13395b4aa86bSJames Feist         }
13405b4aa86bSJames Feist 
134173df0db0SJames Feist         std::shared_ptr<AsyncResp> response = asyncResp;
134273df0db0SJames Feist 
134373df0db0SJames Feist         if (profile)
134473df0db0SJames Feist         {
134573df0db0SJames Feist             if (std::find(supportedProfiles.begin(), supportedProfiles.end(),
134673df0db0SJames Feist                           *profile) == supportedProfiles.end())
134773df0db0SJames Feist             {
134873df0db0SJames Feist                 messages::actionParameterUnknown(response->res, "Profile",
134973df0db0SJames Feist                                                  *profile);
135073df0db0SJames Feist                 return;
135173df0db0SJames Feist             }
135273df0db0SJames Feist             currentProfile = *profile;
135373df0db0SJames Feist             crow::connections::systemBus->async_method_call(
135473df0db0SJames Feist                 [response](const boost::system::error_code ec) {
135573df0db0SJames Feist                     if (ec)
135673df0db0SJames Feist                     {
135773df0db0SJames Feist                         BMCWEB_LOG_ERROR << "Error patching profile" << ec;
135873df0db0SJames Feist                         messages::internalError(response->res);
135973df0db0SJames Feist                     }
136073df0db0SJames Feist                 },
136173df0db0SJames Feist                 profileConnection, profilePath,
136273df0db0SJames Feist                 "org.freedesktop.DBus.Properties", "Set", thermalModeIface,
136373df0db0SJames Feist                 "Current", std::variant<std::string>(*profile));
136473df0db0SJames Feist         }
136573df0db0SJames Feist 
136673df0db0SJames Feist         for (auto& containerPair : configuration)
136773df0db0SJames Feist         {
136873df0db0SJames Feist             auto& container = containerPair.second;
136973df0db0SJames Feist             if (!container)
137073df0db0SJames Feist             {
137173df0db0SJames Feist                 continue;
137273df0db0SJames Feist             }
137373df0db0SJames Feist             std::string& type = containerPair.first;
137473df0db0SJames Feist 
137573df0db0SJames Feist             for (nlohmann::json::iterator it = container->begin();
137673df0db0SJames Feist                  it != container->end(); it++)
137773df0db0SJames Feist             {
137873df0db0SJames Feist                 const auto& name = it.key();
137973df0db0SJames Feist                 auto pathItr =
138073df0db0SJames Feist                     std::find_if(managedObj.begin(), managedObj.end(),
138173df0db0SJames Feist                                  [&name](const auto& obj) {
138273df0db0SJames Feist                                      return boost::algorithm::ends_with(
138373df0db0SJames Feist                                          obj.first.str, "/" + name);
138473df0db0SJames Feist                                  });
138573df0db0SJames Feist                 boost::container::flat_map<std::string,
138673df0db0SJames Feist                                            dbus::utility::DbusVariantType>
138773df0db0SJames Feist                     output;
138873df0db0SJames Feist 
138973df0db0SJames Feist                 output.reserve(16); // The pid interface length
139073df0db0SJames Feist 
139173df0db0SJames Feist                 // determines if we're patching entity-manager or
139273df0db0SJames Feist                 // creating a new object
139373df0db0SJames Feist                 bool createNewObject = (pathItr == managedObj.end());
139473df0db0SJames Feist                 std::string iface;
139573df0db0SJames Feist                 if (type == "PidControllers" || type == "FanControllers")
139673df0db0SJames Feist                 {
139773df0db0SJames Feist                     iface = pidConfigurationIface;
139873df0db0SJames Feist                     if (!createNewObject &&
139973df0db0SJames Feist                         pathItr->second.find(pidConfigurationIface) ==
140073df0db0SJames Feist                             pathItr->second.end())
140173df0db0SJames Feist                     {
140273df0db0SJames Feist                         createNewObject = true;
140373df0db0SJames Feist                     }
140473df0db0SJames Feist                 }
140573df0db0SJames Feist                 else if (type == "FanZones")
140673df0db0SJames Feist                 {
140773df0db0SJames Feist                     iface = pidZoneConfigurationIface;
140873df0db0SJames Feist                     if (!createNewObject &&
140973df0db0SJames Feist                         pathItr->second.find(pidZoneConfigurationIface) ==
141073df0db0SJames Feist                             pathItr->second.end())
141173df0db0SJames Feist                     {
141273df0db0SJames Feist 
141373df0db0SJames Feist                         createNewObject = true;
141473df0db0SJames Feist                     }
141573df0db0SJames Feist                 }
141673df0db0SJames Feist                 else if (type == "StepwiseControllers")
141773df0db0SJames Feist                 {
141873df0db0SJames Feist                     iface = stepwiseConfigurationIface;
141973df0db0SJames Feist                     if (!createNewObject &&
142073df0db0SJames Feist                         pathItr->second.find(stepwiseConfigurationIface) ==
142173df0db0SJames Feist                             pathItr->second.end())
142273df0db0SJames Feist                     {
142373df0db0SJames Feist                         createNewObject = true;
142473df0db0SJames Feist                     }
142573df0db0SJames Feist                 }
142673df0db0SJames Feist                 BMCWEB_LOG_DEBUG << "Create new = " << createNewObject << "\n";
142773df0db0SJames Feist                 output["Name"] = boost::replace_all_copy(name, "_", " ");
142873df0db0SJames Feist 
142973df0db0SJames Feist                 std::string chassis;
143073df0db0SJames Feist                 CreatePIDRet ret = createPidInterface(
143173df0db0SJames Feist                     response, type, it, pathItr->first.str, managedObj,
143273df0db0SJames Feist                     createNewObject, output, chassis, currentProfile);
143373df0db0SJames Feist                 if (ret == CreatePIDRet::fail)
143473df0db0SJames Feist                 {
143573df0db0SJames Feist                     return;
143673df0db0SJames Feist                 }
143773df0db0SJames Feist                 else if (ret == CreatePIDRet::del)
143873df0db0SJames Feist                 {
143973df0db0SJames Feist                     continue;
144073df0db0SJames Feist                 }
144173df0db0SJames Feist 
144273df0db0SJames Feist                 if (!createNewObject)
144373df0db0SJames Feist                 {
144473df0db0SJames Feist                     for (const auto& property : output)
144573df0db0SJames Feist                     {
144673df0db0SJames Feist                         crow::connections::systemBus->async_method_call(
144773df0db0SJames Feist                             [response,
144873df0db0SJames Feist                              propertyName{std::string(property.first)}](
144973df0db0SJames Feist                                 const boost::system::error_code ec) {
145073df0db0SJames Feist                                 if (ec)
145173df0db0SJames Feist                                 {
145273df0db0SJames Feist                                     BMCWEB_LOG_ERROR << "Error patching "
145373df0db0SJames Feist                                                      << propertyName << ": "
145473df0db0SJames Feist                                                      << ec;
145573df0db0SJames Feist                                     messages::internalError(response->res);
145673df0db0SJames Feist                                     return;
145773df0db0SJames Feist                                 }
145873df0db0SJames Feist                                 messages::success(response->res);
145973df0db0SJames Feist                             },
146073df0db0SJames Feist                             "xyz.openbmc_project.EntityManager",
146173df0db0SJames Feist                             pathItr->first.str,
146273df0db0SJames Feist                             "org.freedesktop.DBus.Properties", "Set", iface,
146373df0db0SJames Feist                             property.first, property.second);
146473df0db0SJames Feist                     }
146573df0db0SJames Feist                 }
146673df0db0SJames Feist                 else
146773df0db0SJames Feist                 {
146873df0db0SJames Feist                     if (chassis.empty())
146973df0db0SJames Feist                     {
147073df0db0SJames Feist                         BMCWEB_LOG_ERROR << "Failed to get chassis from config";
147173df0db0SJames Feist                         messages::invalidObject(response->res, name);
147273df0db0SJames Feist                         return;
147373df0db0SJames Feist                     }
147473df0db0SJames Feist 
147573df0db0SJames Feist                     bool foundChassis = false;
147673df0db0SJames Feist                     for (const auto& obj : managedObj)
147773df0db0SJames Feist                     {
147873df0db0SJames Feist                         if (boost::algorithm::ends_with(obj.first.str, chassis))
147973df0db0SJames Feist                         {
148073df0db0SJames Feist                             chassis = obj.first.str;
148173df0db0SJames Feist                             foundChassis = true;
148273df0db0SJames Feist                             break;
148373df0db0SJames Feist                         }
148473df0db0SJames Feist                     }
148573df0db0SJames Feist                     if (!foundChassis)
148673df0db0SJames Feist                     {
148773df0db0SJames Feist                         BMCWEB_LOG_ERROR << "Failed to find chassis on dbus";
148873df0db0SJames Feist                         messages::resourceMissingAtURI(
148973df0db0SJames Feist                             response->res, "/redfish/v1/Chassis/" + chassis);
149073df0db0SJames Feist                         return;
149173df0db0SJames Feist                     }
149273df0db0SJames Feist 
149373df0db0SJames Feist                     crow::connections::systemBus->async_method_call(
149473df0db0SJames Feist                         [response](const boost::system::error_code ec) {
149573df0db0SJames Feist                             if (ec)
149673df0db0SJames Feist                             {
149773df0db0SJames Feist                                 BMCWEB_LOG_ERROR << "Error Adding Pid Object "
149873df0db0SJames Feist                                                  << ec;
149973df0db0SJames Feist                                 messages::internalError(response->res);
150073df0db0SJames Feist                                 return;
150173df0db0SJames Feist                             }
150273df0db0SJames Feist                             messages::success(response->res);
150373df0db0SJames Feist                         },
150473df0db0SJames Feist                         "xyz.openbmc_project.EntityManager", chassis,
150573df0db0SJames Feist                         "xyz.openbmc_project.AddObject", "AddObject", output);
150673df0db0SJames Feist                 }
150773df0db0SJames Feist             }
150873df0db0SJames Feist         }
150973df0db0SJames Feist     }
151073df0db0SJames Feist     std::shared_ptr<AsyncResp> asyncResp;
151173df0db0SJames Feist     std::vector<std::pair<std::string, std::optional<nlohmann::json>>>
151273df0db0SJames Feist         configuration;
151373df0db0SJames Feist     std::optional<std::string> profile;
151473df0db0SJames Feist     dbus::utility::ManagedObjectType managedObj;
151573df0db0SJames Feist     std::vector<std::string> supportedProfiles;
151673df0db0SJames Feist     std::string currentProfile;
151773df0db0SJames Feist     std::string profileConnection;
151873df0db0SJames Feist     std::string profilePath;
151973df0db0SJames Feist };
152073df0db0SJames Feist 
152173df0db0SJames Feist class Manager : public Node
152273df0db0SJames Feist {
152373df0db0SJames Feist   public:
152473df0db0SJames Feist     Manager(CrowApp& app) : Node(app, "/redfish/v1/Managers/bmc/")
152573df0db0SJames Feist     {
152673df0db0SJames Feist         uuid = app.template getMiddleware<crow::persistent_data::Middleware>()
152773df0db0SJames Feist                    .systemUuid;
152873df0db0SJames Feist         entityPrivileges = {
152973df0db0SJames Feist             {boost::beast::http::verb::get, {{"Login"}}},
153073df0db0SJames Feist             {boost::beast::http::verb::head, {{"Login"}}},
153173df0db0SJames Feist             {boost::beast::http::verb::patch, {{"ConfigureManager"}}},
153273df0db0SJames Feist             {boost::beast::http::verb::put, {{"ConfigureManager"}}},
153373df0db0SJames Feist             {boost::beast::http::verb::delete_, {{"ConfigureManager"}}},
153473df0db0SJames Feist             {boost::beast::http::verb::post, {{"ConfigureManager"}}}};
153573df0db0SJames Feist     }
153673df0db0SJames Feist 
153773df0db0SJames Feist   private:
153855c7b7a2SEd Tanous     void doGet(crow::Response& res, const crow::Request& req,
15391abe55efSEd Tanous                const std::vector<std::string>& params) override
15401abe55efSEd Tanous     {
15410f74e643SEd Tanous         res.jsonValue["@odata.id"] = "/redfish/v1/Managers/bmc";
15420f74e643SEd Tanous         res.jsonValue["@odata.type"] = "#Manager.v1_3_0.Manager";
15430f74e643SEd Tanous         res.jsonValue["@odata.context"] =
15440f74e643SEd Tanous             "/redfish/v1/$metadata#Manager.Manager";
15450f74e643SEd Tanous         res.jsonValue["Id"] = "bmc";
15460f74e643SEd Tanous         res.jsonValue["Name"] = "OpenBmc Manager";
15470f74e643SEd Tanous         res.jsonValue["Description"] = "Baseboard Management Controller";
15480f74e643SEd Tanous         res.jsonValue["PowerState"] = "On";
1549029573d4SEd Tanous         res.jsonValue["Status"] = {{"State", "Enabled"}, {"Health", "OK"}};
15500f74e643SEd Tanous         res.jsonValue["ManagerType"] = "BMC";
15513602e232SEd Tanous         res.jsonValue["UUID"] = systemd_utils::getUuid();
15523602e232SEd Tanous         res.jsonValue["ServiceEntryPointUUID"] = uuid;
15530f74e643SEd Tanous         res.jsonValue["Model"] = "OpenBmc"; // TODO(ed), get model
15540f74e643SEd Tanous 
15550f74e643SEd Tanous         res.jsonValue["LogServices"] = {
15560f74e643SEd Tanous             {"@odata.id", "/redfish/v1/Managers/bmc/LogServices"}};
15570f74e643SEd Tanous 
15580f74e643SEd Tanous         res.jsonValue["NetworkProtocol"] = {
15590f74e643SEd Tanous             {"@odata.id", "/redfish/v1/Managers/bmc/NetworkProtocol"}};
15600f74e643SEd Tanous 
15610f74e643SEd Tanous         res.jsonValue["EthernetInterfaces"] = {
15620f74e643SEd Tanous             {"@odata.id", "/redfish/v1/Managers/bmc/EthernetInterfaces"}};
15630f74e643SEd Tanous         // default oem data
15640f74e643SEd Tanous         nlohmann::json& oem = res.jsonValue["Oem"];
15650f74e643SEd Tanous         nlohmann::json& oemOpenbmc = oem["OpenBmc"];
15660f74e643SEd Tanous         oem["@odata.type"] = "#OemManager.Oem";
15670f74e643SEd Tanous         oem["@odata.id"] = "/redfish/v1/Managers/bmc#/Oem";
15680f74e643SEd Tanous         oem["@odata.context"] = "/redfish/v1/$metadata#OemManager.Oem";
15690f74e643SEd Tanous         oemOpenbmc["@odata.type"] = "#OemManager.OpenBmc";
15700f74e643SEd Tanous         oemOpenbmc["@odata.id"] = "/redfish/v1/Managers/bmc#/Oem/OpenBmc";
15710f74e643SEd Tanous         oemOpenbmc["@odata.context"] =
15720f74e643SEd Tanous             "/redfish/v1/$metadata#OemManager.OpenBmc";
15730f74e643SEd Tanous 
1574ed5befbdSJennifer Lee         // Update Actions object.
15750f74e643SEd Tanous         nlohmann::json& manager_reset =
15760f74e643SEd Tanous             res.jsonValue["Actions"]["#Manager.Reset"];
1577ed5befbdSJennifer Lee         manager_reset["target"] =
1578ed5befbdSJennifer Lee             "/redfish/v1/Managers/bmc/Actions/Manager.Reset";
1579ed5befbdSJennifer Lee         manager_reset["ResetType@Redfish.AllowableValues"] = {
1580ed5befbdSJennifer Lee             "GracefulRestart"};
1581ca537928SJennifer Lee 
1582cb92c03bSAndrew Geissler         res.jsonValue["DateTime"] = crow::utility::dateTimeNow();
1583474bfad5SSantosh Puranik 
1584474bfad5SSantosh Puranik         // Fill in GraphicalConsole and SerialConsole info
1585474bfad5SSantosh Puranik         res.jsonValue["SerialConsole"]["ServiceEnabled"] = true;
1586474bfad5SSantosh Puranik         res.jsonValue["SerialConsole"]["ConnectTypesSupported"] = {"IPMI",
1587474bfad5SSantosh Puranik                                                                    "SSH"};
1588ef47bb18SSantosh Puranik #ifdef BMCWEB_ENABLE_KVM
1589ef47bb18SSantosh Puranik         res.jsonValue["GraphicalConsole"]["ServiceEnabled"] = true;
1590ef47bb18SSantosh Puranik         res.jsonValue["GraphicalConsole"]["ConnectTypesSupported"] = {"KVMIP"};
1591ef47bb18SSantosh Puranik #endif // BMCWEB_ENABLE_KVM
1592474bfad5SSantosh Puranik 
1593603a6640SGunnar Mills         res.jsonValue["Links"]["ManagerForServers@odata.count"] = 1;
1594603a6640SGunnar Mills         res.jsonValue["Links"]["ManagerForServers"] = {
1595603a6640SGunnar Mills             {{"@odata.id", "/redfish/v1/Systems/system"}}};
159626f03899SShawn McCarney 
1597ed5befbdSJennifer Lee         std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
15985b4aa86bSJames Feist 
1599b49ac873SJames Feist         auto health = std::make_shared<HealthPopulate>(asyncResp);
1600b49ac873SJames Feist         health->isManagersHealth = true;
1601b49ac873SJames Feist         health->populate();
1602b49ac873SJames Feist 
1603e90c5052SAndrew Geissler         fw_util::getActiveFwVersion(asyncResp, fw_util::bmcPurpose,
1604e90c5052SAndrew Geissler                                     "FirmwareVersion");
16050f6b00bdSJames Feist 
160673df0db0SJames Feist         auto pids = std::make_shared<GetPIDValues>(asyncResp);
160773df0db0SJames Feist         pids->run();
1608c5d03ff4SJennifer Lee 
1609c5d03ff4SJennifer Lee         getMainChassisId(asyncResp, [](const std::string& chassisId,
1610c5d03ff4SJennifer Lee                                        const std::shared_ptr<AsyncResp> aRsp) {
1611c5d03ff4SJennifer Lee             aRsp->res.jsonValue["Links"]["ManagerForChassis@odata.count"] = 1;
1612c5d03ff4SJennifer Lee             aRsp->res.jsonValue["Links"]["ManagerForChassis"] = {
1613c5d03ff4SJennifer Lee                 {{"@odata.id", "/redfish/v1/Chassis/" + chassisId}}};
16142c0feb00SJason M. Bills             aRsp->res.jsonValue["Links"]["ManagerInChassis"] = {
16152c0feb00SJason M. Bills                 {"@odata.id", "/redfish/v1/Chassis/" + chassisId}};
1616c5d03ff4SJennifer Lee         });
16170f6b00bdSJames Feist 
16180f6b00bdSJames Feist         static bool started = false;
16190f6b00bdSJames Feist 
16200f6b00bdSJames Feist         if (!started)
16210f6b00bdSJames Feist         {
16220f6b00bdSJames Feist             crow::connections::systemBus->async_method_call(
16230f6b00bdSJames Feist                 [asyncResp](const boost::system::error_code ec,
16240f6b00bdSJames Feist                             const std::variant<double>& resp) {
16250f6b00bdSJames Feist                     if (ec)
16260f6b00bdSJames Feist                     {
16270f6b00bdSJames Feist                         BMCWEB_LOG_ERROR << "Error while getting progress";
16280f6b00bdSJames Feist                         messages::internalError(asyncResp->res);
16290f6b00bdSJames Feist                         return;
16300f6b00bdSJames Feist                     }
16310f6b00bdSJames Feist                     const double* val = std::get_if<double>(&resp);
16320f6b00bdSJames Feist                     if (val == nullptr)
16330f6b00bdSJames Feist                     {
16340f6b00bdSJames Feist                         BMCWEB_LOG_ERROR
16350f6b00bdSJames Feist                             << "Invalid response while getting progress";
16360f6b00bdSJames Feist                         messages::internalError(asyncResp->res);
16370f6b00bdSJames Feist                         return;
16380f6b00bdSJames Feist                     }
16390f6b00bdSJames Feist                     if (*val < 1.0)
16400f6b00bdSJames Feist                     {
16410f6b00bdSJames Feist                         asyncResp->res.jsonValue["Status"]["State"] =
16420f6b00bdSJames Feist                             "Starting";
16430f6b00bdSJames Feist                         started = true;
16440f6b00bdSJames Feist                     }
16450f6b00bdSJames Feist                 },
16460f6b00bdSJames Feist                 "org.freedesktop.systemd1", "/org/freedesktop/systemd1",
16470f6b00bdSJames Feist                 "org.freedesktop.DBus.Properties", "Get",
16480f6b00bdSJames Feist                 "org.freedesktop.systemd1.Manager", "Progress");
16490f6b00bdSJames Feist         }
165083ff9ab6SJames Feist     }
16515b4aa86bSJames Feist 
16525b4aa86bSJames Feist     void doPatch(crow::Response& res, const crow::Request& req,
16535b4aa86bSJames Feist                  const std::vector<std::string>& params) override
16545b4aa86bSJames Feist     {
16550627a2c7SEd Tanous         std::optional<nlohmann::json> oem;
1656af5d6058SSantosh Puranik         std::optional<std::string> datetime;
16570627a2c7SEd Tanous 
1658af5d6058SSantosh Puranik         if (!json_util::readJson(req, res, "Oem", oem, "DateTime", datetime))
165983ff9ab6SJames Feist         {
166083ff9ab6SJames Feist             return;
166183ff9ab6SJames Feist         }
16620627a2c7SEd Tanous 
166383ff9ab6SJames Feist         std::shared_ptr<AsyncResp> response = std::make_shared<AsyncResp>(res);
16640627a2c7SEd Tanous 
16650627a2c7SEd Tanous         if (oem)
166683ff9ab6SJames Feist         {
16675f2caaefSJames Feist             std::optional<nlohmann::json> openbmc;
166843b761d0SEd Tanous             if (!redfish::json_util::readJson(*oem, res, "OpenBmc", openbmc))
166983ff9ab6SJames Feist             {
167043b761d0SEd Tanous                 BMCWEB_LOG_ERROR << "Line:" << __LINE__ << ", Illegal Property "
167143b761d0SEd Tanous                                  << oem->dump();
167283ff9ab6SJames Feist                 return;
167383ff9ab6SJames Feist             }
16745f2caaefSJames Feist             if (openbmc)
167583ff9ab6SJames Feist             {
16765f2caaefSJames Feist                 std::optional<nlohmann::json> fan;
167743b761d0SEd Tanous                 if (!redfish::json_util::readJson(*openbmc, res, "Fan", fan))
167883ff9ab6SJames Feist                 {
16795f2caaefSJames Feist                     BMCWEB_LOG_ERROR << "Line:" << __LINE__
16805f2caaefSJames Feist                                      << ", Illegal Property "
16815f2caaefSJames Feist                                      << openbmc->dump();
168283ff9ab6SJames Feist                     return;
168383ff9ab6SJames Feist                 }
16845f2caaefSJames Feist                 if (fan)
168583ff9ab6SJames Feist                 {
168673df0db0SJames Feist                     auto pid = std::make_shared<SetPIDValues>(response, *fan);
168773df0db0SJames Feist                     pid->run();
168883ff9ab6SJames Feist                 }
168983ff9ab6SJames Feist             }
169083ff9ab6SJames Feist         }
1691af5d6058SSantosh Puranik         if (datetime)
1692af5d6058SSantosh Puranik         {
1693af5d6058SSantosh Puranik             setDateTime(response, std::move(*datetime));
1694af5d6058SSantosh Puranik         }
1695af5d6058SSantosh Puranik     }
1696af5d6058SSantosh Puranik 
1697af5d6058SSantosh Puranik     void setDateTime(std::shared_ptr<AsyncResp> aResp,
1698af5d6058SSantosh Puranik                      std::string datetime) const
1699af5d6058SSantosh Puranik     {
1700af5d6058SSantosh Puranik         BMCWEB_LOG_DEBUG << "Set date time: " << datetime;
1701af5d6058SSantosh Puranik 
1702af5d6058SSantosh Puranik         std::stringstream stream(datetime);
1703af5d6058SSantosh Puranik         // Convert from ISO 8601 to boost local_time
1704af5d6058SSantosh Puranik         // (BMC only has time in UTC)
1705af5d6058SSantosh Puranik         boost::posix_time::ptime posixTime;
1706af5d6058SSantosh Puranik         boost::posix_time::ptime epoch(boost::gregorian::date(1970, 1, 1));
1707af5d6058SSantosh Puranik         // Facet gets deleted with the stringsteam
1708af5d6058SSantosh Puranik         auto ifc = std::make_unique<boost::local_time::local_time_input_facet>(
1709af5d6058SSantosh Puranik             "%Y-%m-%d %H:%M:%S%F %ZP");
1710af5d6058SSantosh Puranik         stream.imbue(std::locale(stream.getloc(), ifc.release()));
1711af5d6058SSantosh Puranik 
1712af5d6058SSantosh Puranik         boost::local_time::local_date_time ldt(
1713af5d6058SSantosh Puranik             boost::local_time::not_a_date_time);
1714af5d6058SSantosh Puranik 
1715af5d6058SSantosh Puranik         if (stream >> ldt)
1716af5d6058SSantosh Puranik         {
1717af5d6058SSantosh Puranik             posixTime = ldt.utc_time();
1718af5d6058SSantosh Puranik             boost::posix_time::time_duration dur = posixTime - epoch;
1719af5d6058SSantosh Puranik             uint64_t durMicroSecs =
1720af5d6058SSantosh Puranik                 static_cast<uint64_t>(dur.total_microseconds());
1721af5d6058SSantosh Puranik             crow::connections::systemBus->async_method_call(
1722af5d6058SSantosh Puranik                 [aResp{std::move(aResp)}, datetime{std::move(datetime)}](
1723af5d6058SSantosh Puranik                     const boost::system::error_code ec) {
1724af5d6058SSantosh Puranik                     if (ec)
1725af5d6058SSantosh Puranik                     {
1726af5d6058SSantosh Puranik                         BMCWEB_LOG_DEBUG << "Failed to set elapsed time. "
1727af5d6058SSantosh Puranik                                             "DBUS response error "
1728af5d6058SSantosh Puranik                                          << ec;
1729af5d6058SSantosh Puranik                         messages::internalError(aResp->res);
1730af5d6058SSantosh Puranik                         return;
1731af5d6058SSantosh Puranik                     }
1732af5d6058SSantosh Puranik                     aResp->res.jsonValue["DateTime"] = datetime;
1733af5d6058SSantosh Puranik                 },
1734af5d6058SSantosh Puranik                 "xyz.openbmc_project.Time.Manager",
1735af5d6058SSantosh Puranik                 "/xyz/openbmc_project/time/bmc",
1736af5d6058SSantosh Puranik                 "org.freedesktop.DBus.Properties", "Set",
1737af5d6058SSantosh Puranik                 "xyz.openbmc_project.Time.EpochTime", "Elapsed",
1738af5d6058SSantosh Puranik                 std::variant<uint64_t>(durMicroSecs));
1739af5d6058SSantosh Puranik         }
1740af5d6058SSantosh Puranik         else
1741af5d6058SSantosh Puranik         {
1742af5d6058SSantosh Puranik             messages::propertyValueFormatError(aResp->res, datetime,
1743af5d6058SSantosh Puranik                                                "DateTime");
1744af5d6058SSantosh Puranik             return;
1745af5d6058SSantosh Puranik         }
174683ff9ab6SJames Feist     }
17479c310685SBorawski.Lukasz 
17480f74e643SEd Tanous     std::string uuid;
17499c310685SBorawski.Lukasz };
17509c310685SBorawski.Lukasz 
17511abe55efSEd Tanous class ManagerCollection : public Node
17521abe55efSEd Tanous {
17539c310685SBorawski.Lukasz   public:
17541abe55efSEd Tanous     ManagerCollection(CrowApp& app) : Node(app, "/redfish/v1/Managers/")
17551abe55efSEd Tanous     {
1756a434f2bdSEd Tanous         entityPrivileges = {
1757a434f2bdSEd Tanous             {boost::beast::http::verb::get, {{"Login"}}},
1758e0d918bcSEd Tanous             {boost::beast::http::verb::head, {{"Login"}}},
1759e0d918bcSEd Tanous             {boost::beast::http::verb::patch, {{"ConfigureManager"}}},
1760e0d918bcSEd Tanous             {boost::beast::http::verb::put, {{"ConfigureManager"}}},
1761e0d918bcSEd Tanous             {boost::beast::http::verb::delete_, {{"ConfigureManager"}}},
1762e0d918bcSEd Tanous             {boost::beast::http::verb::post, {{"ConfigureManager"}}}};
17639c310685SBorawski.Lukasz     }
17649c310685SBorawski.Lukasz 
17659c310685SBorawski.Lukasz   private:
176655c7b7a2SEd Tanous     void doGet(crow::Response& res, const crow::Request& req,
17671abe55efSEd Tanous                const std::vector<std::string>& params) override
17681abe55efSEd Tanous     {
176983ff9ab6SJames Feist         // Collections don't include the static data added by SubRoute
177083ff9ab6SJames Feist         // because it has a duplicate entry for members
177155c7b7a2SEd Tanous         res.jsonValue["@odata.id"] = "/redfish/v1/Managers";
177255c7b7a2SEd Tanous         res.jsonValue["@odata.type"] = "#ManagerCollection.ManagerCollection";
177355c7b7a2SEd Tanous         res.jsonValue["@odata.context"] =
177455c7b7a2SEd Tanous             "/redfish/v1/$metadata#ManagerCollection.ManagerCollection";
177555c7b7a2SEd Tanous         res.jsonValue["Name"] = "Manager Collection";
177655c7b7a2SEd Tanous         res.jsonValue["Members@odata.count"] = 1;
177755c7b7a2SEd Tanous         res.jsonValue["Members"] = {
17785b4aa86bSJames Feist             {{"@odata.id", "/redfish/v1/Managers/bmc"}}};
17799c310685SBorawski.Lukasz         res.end();
17809c310685SBorawski.Lukasz     }
17819c310685SBorawski.Lukasz };
17829c310685SBorawski.Lukasz } // namespace redfish
1783