xref: /openbmc/bmcweb/features/redfish/lib/managers.hpp (revision 0f6b00bded6e4c4b4e1577644e52cc33cb73bb2d)
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 
189c310685SBorawski.Lukasz #include "node.hpp"
19c5d03ff4SJennifer Lee #include "redfish_util.hpp"
209c310685SBorawski.Lukasz 
215b4aa86bSJames Feist #include <boost/algorithm/string/replace.hpp>
22af5d6058SSantosh Puranik #include <boost/date_time.hpp>
235b4aa86bSJames Feist #include <dbus_utility.hpp>
2473df0db0SJames Feist #include <memory>
25af5d6058SSantosh Puranik #include <sstream>
267bffdb7eSBernard Wong #include <utils/systemd_utils.hpp>
27abf2add6SEd Tanous #include <variant>
285b4aa86bSJames Feist 
291abe55efSEd Tanous namespace redfish
301abe55efSEd Tanous {
31ed5befbdSJennifer Lee 
32ed5befbdSJennifer Lee /**
33ed5befbdSJennifer Lee  * ManagerActionsReset class supports handle POST method for Reset action.
34ed5befbdSJennifer Lee  * The class retrieves and sends data directly to dbus.
35ed5befbdSJennifer Lee  */
36ed5befbdSJennifer Lee class ManagerActionsReset : public Node
37ed5befbdSJennifer Lee {
38ed5befbdSJennifer Lee   public:
39ed5befbdSJennifer Lee     ManagerActionsReset(CrowApp& app) :
40ed5befbdSJennifer Lee         Node(app, "/redfish/v1/Managers/bmc/Actions/Manager.Reset/")
41ed5befbdSJennifer Lee     {
42ed5befbdSJennifer Lee         entityPrivileges = {
43ed5befbdSJennifer Lee             {boost::beast::http::verb::get, {{"Login"}}},
44ed5befbdSJennifer Lee             {boost::beast::http::verb::head, {{"Login"}}},
45ed5befbdSJennifer Lee             {boost::beast::http::verb::patch, {{"ConfigureManager"}}},
46ed5befbdSJennifer Lee             {boost::beast::http::verb::put, {{"ConfigureManager"}}},
47ed5befbdSJennifer Lee             {boost::beast::http::verb::delete_, {{"ConfigureManager"}}},
48ed5befbdSJennifer Lee             {boost::beast::http::verb::post, {{"ConfigureManager"}}}};
49ed5befbdSJennifer Lee     }
50ed5befbdSJennifer Lee 
51ed5befbdSJennifer Lee   private:
52ed5befbdSJennifer Lee     /**
53ed5befbdSJennifer Lee      * Function handles POST method request.
54ed5befbdSJennifer Lee      * Analyzes POST body message before sends Reset request data to dbus.
55ed5befbdSJennifer Lee      * OpenBMC allows for ResetType is GracefulRestart only.
56ed5befbdSJennifer Lee      */
57ed5befbdSJennifer Lee     void doPost(crow::Response& res, const crow::Request& req,
58ed5befbdSJennifer Lee                 const std::vector<std::string>& params) override
59ed5befbdSJennifer Lee     {
60ed5befbdSJennifer Lee         std::string resetType;
61ed5befbdSJennifer Lee 
62ed5befbdSJennifer Lee         if (!json_util::readJson(req, res, "ResetType", resetType))
63ed5befbdSJennifer Lee         {
64ed5befbdSJennifer Lee             return;
65ed5befbdSJennifer Lee         }
66ed5befbdSJennifer Lee 
67ed5befbdSJennifer Lee         if (resetType != "GracefulRestart")
68ed5befbdSJennifer Lee         {
69ed5befbdSJennifer Lee             res.result(boost::beast::http::status::bad_request);
70ed5befbdSJennifer Lee             messages::actionParameterNotSupported(res, resetType, "ResetType");
71ed5befbdSJennifer Lee             BMCWEB_LOG_ERROR << "Request incorrect action parameter: "
72ed5befbdSJennifer Lee                              << resetType;
73ed5befbdSJennifer Lee             res.end();
74ed5befbdSJennifer Lee             return;
75ed5befbdSJennifer Lee         }
76ed5befbdSJennifer Lee         doBMCGracefulRestart(res, req, params);
77ed5befbdSJennifer Lee     }
78ed5befbdSJennifer Lee 
79ed5befbdSJennifer Lee     /**
80ed5befbdSJennifer Lee      * Function transceives data with dbus directly.
81ed5befbdSJennifer Lee      * All BMC state properties will be retrieved before sending reset request.
82ed5befbdSJennifer Lee      */
83ed5befbdSJennifer Lee     void doBMCGracefulRestart(crow::Response& res, const crow::Request& req,
84ed5befbdSJennifer Lee                               const std::vector<std::string>& params)
85ed5befbdSJennifer Lee     {
86ed5befbdSJennifer Lee         const char* processName = "xyz.openbmc_project.State.BMC";
87ed5befbdSJennifer Lee         const char* objectPath = "/xyz/openbmc_project/state/bmc0";
88ed5befbdSJennifer Lee         const char* interfaceName = "xyz.openbmc_project.State.BMC";
89ed5befbdSJennifer Lee         const std::string& propertyValue =
90ed5befbdSJennifer Lee             "xyz.openbmc_project.State.BMC.Transition.Reboot";
91ed5befbdSJennifer Lee         const char* destProperty = "RequestedBMCTransition";
92ed5befbdSJennifer Lee 
93ed5befbdSJennifer Lee         // Create the D-Bus variant for D-Bus call.
94ed5befbdSJennifer Lee         VariantType dbusPropertyValue(propertyValue);
95ed5befbdSJennifer Lee 
96ed5befbdSJennifer Lee         std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
97ed5befbdSJennifer Lee 
98ed5befbdSJennifer Lee         crow::connections::systemBus->async_method_call(
99ed5befbdSJennifer Lee             [asyncResp](const boost::system::error_code ec) {
100ed5befbdSJennifer Lee                 // Use "Set" method to set the property value.
101ed5befbdSJennifer Lee                 if (ec)
102ed5befbdSJennifer Lee                 {
103ed5befbdSJennifer Lee                     BMCWEB_LOG_ERROR << "[Set] Bad D-Bus request error: " << ec;
104ed5befbdSJennifer Lee                     messages::internalError(asyncResp->res);
105ed5befbdSJennifer Lee                     return;
106ed5befbdSJennifer Lee                 }
107ed5befbdSJennifer Lee 
108ed5befbdSJennifer Lee                 messages::success(asyncResp->res);
109ed5befbdSJennifer Lee             },
110ed5befbdSJennifer Lee             processName, objectPath, "org.freedesktop.DBus.Properties", "Set",
111ed5befbdSJennifer Lee             interfaceName, destProperty, dbusPropertyValue);
112ed5befbdSJennifer Lee     }
113ed5befbdSJennifer Lee };
114ed5befbdSJennifer Lee 
1155b4aa86bSJames Feist static constexpr const char* objectManagerIface =
1165b4aa86bSJames Feist     "org.freedesktop.DBus.ObjectManager";
1175b4aa86bSJames Feist static constexpr const char* pidConfigurationIface =
1185b4aa86bSJames Feist     "xyz.openbmc_project.Configuration.Pid";
1195b4aa86bSJames Feist static constexpr const char* pidZoneConfigurationIface =
1205b4aa86bSJames Feist     "xyz.openbmc_project.Configuration.Pid.Zone";
121b7a08d04SJames Feist static constexpr const char* stepwiseConfigurationIface =
122b7a08d04SJames Feist     "xyz.openbmc_project.Configuration.Stepwise";
12373df0db0SJames Feist static constexpr const char* thermalModeIface =
12473df0db0SJames Feist     "xyz.openbmc_project.Control.ThermalMode";
1259c310685SBorawski.Lukasz 
1265b4aa86bSJames Feist static void asyncPopulatePid(const std::string& connection,
1275b4aa86bSJames Feist                              const std::string& path,
12873df0db0SJames Feist                              const std::string& currentProfile,
12973df0db0SJames Feist                              const std::vector<std::string>& supportedProfiles,
1305b4aa86bSJames Feist                              std::shared_ptr<AsyncResp> asyncResp)
1315b4aa86bSJames Feist {
1325b4aa86bSJames Feist 
1335b4aa86bSJames Feist     crow::connections::systemBus->async_method_call(
13473df0db0SJames Feist         [asyncResp, currentProfile, supportedProfiles](
13573df0db0SJames Feist             const boost::system::error_code ec,
1365b4aa86bSJames Feist             const dbus::utility::ManagedObjectType& managedObj) {
1375b4aa86bSJames Feist             if (ec)
1385b4aa86bSJames Feist             {
1395b4aa86bSJames Feist                 BMCWEB_LOG_ERROR << ec;
1405b4aa86bSJames Feist                 asyncResp->res.jsonValue.clear();
141f12894f8SJason M. Bills                 messages::internalError(asyncResp->res);
1425b4aa86bSJames Feist                 return;
1435b4aa86bSJames Feist             }
1445b4aa86bSJames Feist             nlohmann::json& configRoot =
1455b4aa86bSJames Feist                 asyncResp->res.jsonValue["Oem"]["OpenBmc"]["Fan"];
1465b4aa86bSJames Feist             nlohmann::json& fans = configRoot["FanControllers"];
1475b4aa86bSJames Feist             fans["@odata.type"] = "#OemManager.FanControllers";
1485b4aa86bSJames Feist             fans["@odata.context"] =
1495b4aa86bSJames Feist                 "/redfish/v1/$metadata#OemManager.FanControllers";
1505b4aa86bSJames Feist             fans["@odata.id"] = "/redfish/v1/Managers/bmc#/Oem/OpenBmc/"
1515b4aa86bSJames Feist                                 "Fan/FanControllers";
1525b4aa86bSJames Feist 
1535b4aa86bSJames Feist             nlohmann::json& pids = configRoot["PidControllers"];
1545b4aa86bSJames Feist             pids["@odata.type"] = "#OemManager.PidControllers";
1555b4aa86bSJames Feist             pids["@odata.context"] =
1565b4aa86bSJames Feist                 "/redfish/v1/$metadata#OemManager.PidControllers";
1575b4aa86bSJames Feist             pids["@odata.id"] =
1585b4aa86bSJames Feist                 "/redfish/v1/Managers/bmc#/Oem/OpenBmc/Fan/PidControllers";
1595b4aa86bSJames Feist 
160b7a08d04SJames Feist             nlohmann::json& stepwise = configRoot["StepwiseControllers"];
161b7a08d04SJames Feist             stepwise["@odata.type"] = "#OemManager.StepwiseControllers";
162b7a08d04SJames Feist             stepwise["@odata.context"] =
163b7a08d04SJames Feist                 "/redfish/v1/$metadata#OemManager.StepwiseControllers";
164b7a08d04SJames Feist             stepwise["@odata.id"] =
165b7a08d04SJames Feist                 "/redfish/v1/Managers/bmc#/Oem/OpenBmc/Fan/StepwiseControllers";
166b7a08d04SJames Feist 
1675b4aa86bSJames Feist             nlohmann::json& zones = configRoot["FanZones"];
1685b4aa86bSJames Feist             zones["@odata.id"] =
1695b4aa86bSJames Feist                 "/redfish/v1/Managers/bmc#/Oem/OpenBmc/Fan/FanZones";
1705b4aa86bSJames Feist             zones["@odata.type"] = "#OemManager.FanZones";
1715b4aa86bSJames Feist             zones["@odata.context"] =
1725b4aa86bSJames Feist                 "/redfish/v1/$metadata#OemManager.FanZones";
1735b4aa86bSJames Feist             configRoot["@odata.id"] =
1745b4aa86bSJames Feist                 "/redfish/v1/Managers/bmc#/Oem/OpenBmc/Fan";
1755b4aa86bSJames Feist             configRoot["@odata.type"] = "#OemManager.Fan";
1765b4aa86bSJames Feist             configRoot["@odata.context"] =
1775b4aa86bSJames Feist                 "/redfish/v1/$metadata#OemManager.Fan";
17873df0db0SJames Feist             configRoot["Profile@Redfish.AllowableValues"] = supportedProfiles;
17973df0db0SJames Feist 
18073df0db0SJames Feist             if (!currentProfile.empty())
18173df0db0SJames Feist             {
18273df0db0SJames Feist                 configRoot["Profile"] = currentProfile;
18373df0db0SJames Feist             }
18473df0db0SJames Feist             BMCWEB_LOG_ERROR << "profile = " << currentProfile << " !";
1855b4aa86bSJames Feist 
1865b4aa86bSJames Feist             for (const auto& pathPair : managedObj)
1875b4aa86bSJames Feist             {
1885b4aa86bSJames Feist                 for (const auto& intfPair : pathPair.second)
1895b4aa86bSJames Feist                 {
1905b4aa86bSJames Feist                     if (intfPair.first != pidConfigurationIface &&
191b7a08d04SJames Feist                         intfPair.first != pidZoneConfigurationIface &&
192b7a08d04SJames Feist                         intfPair.first != stepwiseConfigurationIface)
1935b4aa86bSJames Feist                     {
1945b4aa86bSJames Feist                         continue;
1955b4aa86bSJames Feist                     }
1965b4aa86bSJames Feist                     auto findName = intfPair.second.find("Name");
1975b4aa86bSJames Feist                     if (findName == intfPair.second.end())
1985b4aa86bSJames Feist                     {
1995b4aa86bSJames Feist                         BMCWEB_LOG_ERROR << "Pid Field missing Name";
200a08b46ccSJason M. Bills                         messages::internalError(asyncResp->res);
2015b4aa86bSJames Feist                         return;
2025b4aa86bSJames Feist                     }
20373df0db0SJames Feist 
2045b4aa86bSJames Feist                     const std::string* namePtr =
205abf2add6SEd Tanous                         std::get_if<std::string>(&findName->second);
2065b4aa86bSJames Feist                     if (namePtr == nullptr)
2075b4aa86bSJames Feist                     {
2085b4aa86bSJames Feist                         BMCWEB_LOG_ERROR << "Pid Name Field illegal";
209b7a08d04SJames Feist                         messages::internalError(asyncResp->res);
2105b4aa86bSJames Feist                         return;
2115b4aa86bSJames Feist                     }
2125b4aa86bSJames Feist                     std::string name = *namePtr;
2135b4aa86bSJames Feist                     dbus::utility::escapePathForDbus(name);
21473df0db0SJames Feist 
21573df0db0SJames Feist                     auto findProfiles = intfPair.second.find("Profiles");
21673df0db0SJames Feist                     if (findProfiles != intfPair.second.end())
21773df0db0SJames Feist                     {
21873df0db0SJames Feist                         const std::vector<std::string>* profiles =
21973df0db0SJames Feist                             std::get_if<std::vector<std::string>>(
22073df0db0SJames Feist                                 &findProfiles->second);
22173df0db0SJames Feist                         if (profiles == nullptr)
22273df0db0SJames Feist                         {
22373df0db0SJames Feist                             BMCWEB_LOG_ERROR << "Pid Profiles Field illegal";
22473df0db0SJames Feist                             messages::internalError(asyncResp->res);
22573df0db0SJames Feist                             return;
22673df0db0SJames Feist                         }
22773df0db0SJames Feist                         if (std::find(profiles->begin(), profiles->end(),
22873df0db0SJames Feist                                       currentProfile) == profiles->end())
22973df0db0SJames Feist                         {
23073df0db0SJames Feist                             BMCWEB_LOG_INFO
23173df0db0SJames Feist                                 << name << " not supported in current profile";
23273df0db0SJames Feist                             continue;
23373df0db0SJames Feist                         }
23473df0db0SJames Feist                     }
235b7a08d04SJames Feist                     nlohmann::json* config = nullptr;
236c33a90ecSJames Feist 
237c33a90ecSJames Feist                     const std::string* classPtr = nullptr;
238c33a90ecSJames Feist                     auto findClass = intfPair.second.find("Class");
239c33a90ecSJames Feist                     if (findClass != intfPair.second.end())
240c33a90ecSJames Feist                     {
241c33a90ecSJames Feist                         classPtr = std::get_if<std::string>(&findClass->second);
242c33a90ecSJames Feist                     }
243c33a90ecSJames Feist 
2445b4aa86bSJames Feist                     if (intfPair.first == pidZoneConfigurationIface)
2455b4aa86bSJames Feist                     {
2465b4aa86bSJames Feist                         std::string chassis;
2475b4aa86bSJames Feist                         if (!dbus::utility::getNthStringFromPath(
2485b4aa86bSJames Feist                                 pathPair.first.str, 5, chassis))
2495b4aa86bSJames Feist                         {
2505b4aa86bSJames Feist                             chassis = "#IllegalValue";
2515b4aa86bSJames Feist                         }
2525b4aa86bSJames Feist                         nlohmann::json& zone = zones[name];
2535b4aa86bSJames Feist                         zone["Chassis"] = {
2545b4aa86bSJames Feist                             {"@odata.id", "/redfish/v1/Chassis/" + chassis}};
2555b4aa86bSJames Feist                         zone["@odata.id"] = "/redfish/v1/Managers/bmc#/Oem/"
2565b4aa86bSJames Feist                                             "OpenBmc/Fan/FanZones/" +
2575b4aa86bSJames Feist                                             name;
2585b4aa86bSJames Feist                         zone["@odata.type"] = "#OemManager.FanZone";
2595b4aa86bSJames Feist                         zone["@odata.context"] =
2605b4aa86bSJames Feist                             "/redfish/v1/$metadata#OemManager.FanZone";
261b7a08d04SJames Feist                         config = &zone;
2625b4aa86bSJames Feist                     }
2635b4aa86bSJames Feist 
264b7a08d04SJames Feist                     else if (intfPair.first == stepwiseConfigurationIface)
2655b4aa86bSJames Feist                     {
266c33a90ecSJames Feist                         if (classPtr == nullptr)
267c33a90ecSJames Feist                         {
268c33a90ecSJames Feist                             BMCWEB_LOG_ERROR << "Pid Class Field illegal";
269c33a90ecSJames Feist                             messages::internalError(asyncResp->res);
270c33a90ecSJames Feist                             return;
271c33a90ecSJames Feist                         }
272c33a90ecSJames Feist 
273b7a08d04SJames Feist                         nlohmann::json& controller = stepwise[name];
274b7a08d04SJames Feist                         config = &controller;
2755b4aa86bSJames Feist 
276b7a08d04SJames Feist                         controller["@odata.id"] =
277b7a08d04SJames Feist                             "/redfish/v1/Managers/bmc#/Oem/"
278b7a08d04SJames Feist                             "OpenBmc/Fan/StepwiseControllers/" +
279b7a08d04SJames Feist                             std::string(name);
280b7a08d04SJames Feist                         controller["@odata.type"] =
281b7a08d04SJames Feist                             "#OemManager.StepwiseController";
282b7a08d04SJames Feist 
283b7a08d04SJames Feist                         controller["@odata.context"] =
284b7a08d04SJames Feist                             "/redfish/v1/"
285b7a08d04SJames Feist                             "$metadata#OemManager.StepwiseController";
286c33a90ecSJames Feist                         controller["Direction"] = *classPtr;
2875b4aa86bSJames Feist                     }
2885b4aa86bSJames Feist 
2895b4aa86bSJames Feist                     // pid and fans are off the same configuration
290b7a08d04SJames Feist                     else if (intfPair.first == pidConfigurationIface)
2915b4aa86bSJames Feist                     {
292c33a90ecSJames Feist 
2935b4aa86bSJames Feist                         if (classPtr == nullptr)
2945b4aa86bSJames Feist                         {
2955b4aa86bSJames Feist                             BMCWEB_LOG_ERROR << "Pid Class Field illegal";
296a08b46ccSJason M. Bills                             messages::internalError(asyncResp->res);
2975b4aa86bSJames Feist                             return;
2985b4aa86bSJames Feist                         }
2995b4aa86bSJames Feist                         bool isFan = *classPtr == "fan";
3005b4aa86bSJames Feist                         nlohmann::json& element =
3015b4aa86bSJames Feist                             isFan ? fans[name] : pids[name];
302b7a08d04SJames Feist                         config = &element;
3035b4aa86bSJames Feist                         if (isFan)
3045b4aa86bSJames Feist                         {
3055b4aa86bSJames Feist                             element["@odata.id"] =
3065b4aa86bSJames Feist                                 "/redfish/v1/Managers/bmc#/Oem/"
3075b4aa86bSJames Feist                                 "OpenBmc/Fan/FanControllers/" +
3085b4aa86bSJames Feist                                 std::string(name);
3095b4aa86bSJames Feist                             element["@odata.type"] =
3105b4aa86bSJames Feist                                 "#OemManager.FanController";
3115b4aa86bSJames Feist 
3125b4aa86bSJames Feist                             element["@odata.context"] =
3135b4aa86bSJames Feist                                 "/redfish/v1/"
3145b4aa86bSJames Feist                                 "$metadata#OemManager.FanController";
3155b4aa86bSJames Feist                         }
3165b4aa86bSJames Feist                         else
3175b4aa86bSJames Feist                         {
3185b4aa86bSJames Feist                             element["@odata.id"] =
3195b4aa86bSJames Feist                                 "/redfish/v1/Managers/bmc#/Oem/"
3205b4aa86bSJames Feist                                 "OpenBmc/Fan/PidControllers/" +
3215b4aa86bSJames Feist                                 std::string(name);
3225b4aa86bSJames Feist                             element["@odata.type"] =
3235b4aa86bSJames Feist                                 "#OemManager.PidController";
3245b4aa86bSJames Feist                             element["@odata.context"] =
3255b4aa86bSJames Feist                                 "/redfish/v1/$metadata"
3265b4aa86bSJames Feist                                 "#OemManager.PidController";
3275b4aa86bSJames Feist                         }
328b7a08d04SJames Feist                     }
329b7a08d04SJames Feist                     else
330b7a08d04SJames Feist                     {
331b7a08d04SJames Feist                         BMCWEB_LOG_ERROR << "Unexpected configuration";
332b7a08d04SJames Feist                         messages::internalError(asyncResp->res);
333b7a08d04SJames Feist                         return;
334b7a08d04SJames Feist                     }
335b7a08d04SJames Feist 
336b7a08d04SJames Feist                     // used for making maps out of 2 vectors
337b7a08d04SJames Feist                     const std::vector<double>* keys = nullptr;
338b7a08d04SJames Feist                     const std::vector<double>* values = nullptr;
339b7a08d04SJames Feist 
340b7a08d04SJames Feist                     for (const auto& propertyPair : intfPair.second)
341b7a08d04SJames Feist                     {
342b7a08d04SJames Feist                         if (propertyPair.first == "Type" ||
343b7a08d04SJames Feist                             propertyPair.first == "Class" ||
344b7a08d04SJames Feist                             propertyPair.first == "Name")
345b7a08d04SJames Feist                         {
346b7a08d04SJames Feist                             continue;
347b7a08d04SJames Feist                         }
348b7a08d04SJames Feist 
349b7a08d04SJames Feist                         // zones
350b7a08d04SJames Feist                         if (intfPair.first == pidZoneConfigurationIface)
351b7a08d04SJames Feist                         {
352b7a08d04SJames Feist                             const double* ptr =
353abf2add6SEd Tanous                                 std::get_if<double>(&propertyPair.second);
354b7a08d04SJames Feist                             if (ptr == nullptr)
355b7a08d04SJames Feist                             {
356b7a08d04SJames Feist                                 BMCWEB_LOG_ERROR << "Field Illegal "
357b7a08d04SJames Feist                                                  << propertyPair.first;
358b7a08d04SJames Feist                                 messages::internalError(asyncResp->res);
359b7a08d04SJames Feist                                 return;
360b7a08d04SJames Feist                             }
361b7a08d04SJames Feist                             (*config)[propertyPair.first] = *ptr;
362b7a08d04SJames Feist                         }
363b7a08d04SJames Feist 
364b7a08d04SJames Feist                         if (intfPair.first == stepwiseConfigurationIface)
365b7a08d04SJames Feist                         {
366b7a08d04SJames Feist                             if (propertyPair.first == "Reading" ||
367b7a08d04SJames Feist                                 propertyPair.first == "Output")
368b7a08d04SJames Feist                             {
369b7a08d04SJames Feist                                 const std::vector<double>* ptr =
370abf2add6SEd Tanous                                     std::get_if<std::vector<double>>(
371b7a08d04SJames Feist                                         &propertyPair.second);
372b7a08d04SJames Feist 
373b7a08d04SJames Feist                                 if (ptr == nullptr)
374b7a08d04SJames Feist                                 {
375b7a08d04SJames Feist                                     BMCWEB_LOG_ERROR << "Field Illegal "
376b7a08d04SJames Feist                                                      << propertyPair.first;
377b7a08d04SJames Feist                                     messages::internalError(asyncResp->res);
378b7a08d04SJames Feist                                     return;
379b7a08d04SJames Feist                                 }
380b7a08d04SJames Feist 
381b7a08d04SJames Feist                                 if (propertyPair.first == "Reading")
382b7a08d04SJames Feist                                 {
383b7a08d04SJames Feist                                     keys = ptr;
384b7a08d04SJames Feist                                 }
385b7a08d04SJames Feist                                 else
386b7a08d04SJames Feist                                 {
387b7a08d04SJames Feist                                     values = ptr;
388b7a08d04SJames Feist                                 }
389b7a08d04SJames Feist                                 if (keys && values)
390b7a08d04SJames Feist                                 {
391b7a08d04SJames Feist                                     if (keys->size() != values->size())
392b7a08d04SJames Feist                                     {
393b7a08d04SJames Feist                                         BMCWEB_LOG_ERROR
394b7a08d04SJames Feist                                             << "Reading and Output size don't "
395b7a08d04SJames Feist                                                "match ";
396b7a08d04SJames Feist                                         messages::internalError(asyncResp->res);
397b7a08d04SJames Feist                                         return;
398b7a08d04SJames Feist                                     }
399b7a08d04SJames Feist                                     nlohmann::json& steps = (*config)["Steps"];
400b7a08d04SJames Feist                                     steps = nlohmann::json::array();
401b7a08d04SJames Feist                                     for (size_t ii = 0; ii < keys->size(); ii++)
402b7a08d04SJames Feist                                     {
403b7a08d04SJames Feist                                         steps.push_back(
404b7a08d04SJames Feist                                             {{"Target", (*keys)[ii]},
405b7a08d04SJames Feist                                              {"Output", (*values)[ii]}});
406b7a08d04SJames Feist                                     }
407b7a08d04SJames Feist                                 }
408b7a08d04SJames Feist                             }
409b7a08d04SJames Feist                             if (propertyPair.first == "NegativeHysteresis" ||
410b7a08d04SJames Feist                                 propertyPair.first == "PositiveHysteresis")
411b7a08d04SJames Feist                             {
412b7a08d04SJames Feist                                 const double* ptr =
413abf2add6SEd Tanous                                     std::get_if<double>(&propertyPair.second);
414b7a08d04SJames Feist                                 if (ptr == nullptr)
415b7a08d04SJames Feist                                 {
416b7a08d04SJames Feist                                     BMCWEB_LOG_ERROR << "Field Illegal "
417b7a08d04SJames Feist                                                      << propertyPair.first;
418b7a08d04SJames Feist                                     messages::internalError(asyncResp->res);
419b7a08d04SJames Feist                                     return;
420b7a08d04SJames Feist                                 }
421b7a08d04SJames Feist                                 (*config)[propertyPair.first] = *ptr;
422b7a08d04SJames Feist                             }
423b7a08d04SJames Feist                         }
424b7a08d04SJames Feist 
425b7a08d04SJames Feist                         // pid and fans are off the same configuration
426b7a08d04SJames Feist                         if (intfPair.first == pidConfigurationIface ||
427b7a08d04SJames Feist                             intfPair.first == stepwiseConfigurationIface)
428b7a08d04SJames Feist                         {
4295b4aa86bSJames Feist 
4305b4aa86bSJames Feist                             if (propertyPair.first == "Zones")
4315b4aa86bSJames Feist                             {
4325b4aa86bSJames Feist                                 const std::vector<std::string>* inputs =
433abf2add6SEd Tanous                                     std::get_if<std::vector<std::string>>(
4341b6b96c5SEd Tanous                                         &propertyPair.second);
4355b4aa86bSJames Feist 
4365b4aa86bSJames Feist                                 if (inputs == nullptr)
4375b4aa86bSJames Feist                                 {
4385b4aa86bSJames Feist                                     BMCWEB_LOG_ERROR
4395b4aa86bSJames Feist                                         << "Zones Pid Field Illegal";
440a08b46ccSJason M. Bills                                     messages::internalError(asyncResp->res);
4415b4aa86bSJames Feist                                     return;
4425b4aa86bSJames Feist                                 }
443b7a08d04SJames Feist                                 auto& data = (*config)[propertyPair.first];
4445b4aa86bSJames Feist                                 data = nlohmann::json::array();
4455b4aa86bSJames Feist                                 for (std::string itemCopy : *inputs)
4465b4aa86bSJames Feist                                 {
4475b4aa86bSJames Feist                                     dbus::utility::escapePathForDbus(itemCopy);
4485b4aa86bSJames Feist                                     data.push_back(
4495b4aa86bSJames Feist                                         {{"@odata.id",
4505b4aa86bSJames Feist                                           "/redfish/v1/Managers/bmc#/Oem/"
4515b4aa86bSJames Feist                                           "OpenBmc/Fan/FanZones/" +
4525b4aa86bSJames Feist                                               itemCopy}});
4535b4aa86bSJames Feist                                 }
4545b4aa86bSJames Feist                             }
4555b4aa86bSJames Feist                             // todo(james): may never happen, but this
4565b4aa86bSJames Feist                             // assumes configuration data referenced in the
4575b4aa86bSJames Feist                             // PID config is provided by the same daemon, we
4585b4aa86bSJames Feist                             // could add another loop to cover all cases,
4595b4aa86bSJames Feist                             // but I'm okay kicking this can down the road a
4605b4aa86bSJames Feist                             // bit
4615b4aa86bSJames Feist 
4625b4aa86bSJames Feist                             else if (propertyPair.first == "Inputs" ||
4635b4aa86bSJames Feist                                      propertyPair.first == "Outputs")
4645b4aa86bSJames Feist                             {
465b7a08d04SJames Feist                                 auto& data = (*config)[propertyPair.first];
4665b4aa86bSJames Feist                                 const std::vector<std::string>* inputs =
467abf2add6SEd Tanous                                     std::get_if<std::vector<std::string>>(
4681b6b96c5SEd Tanous                                         &propertyPair.second);
4695b4aa86bSJames Feist 
4705b4aa86bSJames Feist                                 if (inputs == nullptr)
4715b4aa86bSJames Feist                                 {
4725b4aa86bSJames Feist                                     BMCWEB_LOG_ERROR << "Field Illegal "
4735b4aa86bSJames Feist                                                      << propertyPair.first;
474f12894f8SJason M. Bills                                     messages::internalError(asyncResp->res);
4755b4aa86bSJames Feist                                     return;
4765b4aa86bSJames Feist                                 }
4775b4aa86bSJames Feist                                 data = *inputs;
4785b4aa86bSJames Feist                             } // doubles
4795b4aa86bSJames Feist                             else if (propertyPair.first ==
4805b4aa86bSJames Feist                                          "FFGainCoefficient" ||
4815b4aa86bSJames Feist                                      propertyPair.first == "FFOffCoefficient" ||
4825b4aa86bSJames Feist                                      propertyPair.first == "ICoefficient" ||
4835b4aa86bSJames Feist                                      propertyPair.first == "ILimitMax" ||
4845b4aa86bSJames Feist                                      propertyPair.first == "ILimitMin" ||
485aad1a257SJames Feist                                      propertyPair.first ==
486aad1a257SJames Feist                                          "PositiveHysteresis" ||
487aad1a257SJames Feist                                      propertyPair.first ==
488aad1a257SJames Feist                                          "NegativeHysteresis" ||
4895b4aa86bSJames Feist                                      propertyPair.first == "OutLimitMax" ||
4905b4aa86bSJames Feist                                      propertyPair.first == "OutLimitMin" ||
4915b4aa86bSJames Feist                                      propertyPair.first == "PCoefficient" ||
4927625cb81SJames Feist                                      propertyPair.first == "SetPoint" ||
4935b4aa86bSJames Feist                                      propertyPair.first == "SlewNeg" ||
4945b4aa86bSJames Feist                                      propertyPair.first == "SlewPos")
4955b4aa86bSJames Feist                             {
4965b4aa86bSJames Feist                                 const double* ptr =
497abf2add6SEd Tanous                                     std::get_if<double>(&propertyPair.second);
4985b4aa86bSJames Feist                                 if (ptr == nullptr)
4995b4aa86bSJames Feist                                 {
5005b4aa86bSJames Feist                                     BMCWEB_LOG_ERROR << "Field Illegal "
5015b4aa86bSJames Feist                                                      << propertyPair.first;
502f12894f8SJason M. Bills                                     messages::internalError(asyncResp->res);
5035b4aa86bSJames Feist                                     return;
5045b4aa86bSJames Feist                                 }
505b7a08d04SJames Feist                                 (*config)[propertyPair.first] = *ptr;
5065b4aa86bSJames Feist                             }
5075b4aa86bSJames Feist                         }
5085b4aa86bSJames Feist                     }
5095b4aa86bSJames Feist                 }
5105b4aa86bSJames Feist             }
5115b4aa86bSJames Feist         },
5125b4aa86bSJames Feist         connection, path, objectManagerIface, "GetManagedObjects");
5135b4aa86bSJames Feist }
514ca537928SJennifer Lee 
51583ff9ab6SJames Feist enum class CreatePIDRet
51683ff9ab6SJames Feist {
51783ff9ab6SJames Feist     fail,
51883ff9ab6SJames Feist     del,
51983ff9ab6SJames Feist     patch
52083ff9ab6SJames Feist };
52183ff9ab6SJames Feist 
5225f2caaefSJames Feist static bool getZonesFromJsonReq(const std::shared_ptr<AsyncResp>& response,
5235f2caaefSJames Feist                                 std::vector<nlohmann::json>& config,
5245f2caaefSJames Feist                                 std::vector<std::string>& zones)
5255f2caaefSJames Feist {
526b6baeaa4SJames Feist     if (config.empty())
527b6baeaa4SJames Feist     {
528b6baeaa4SJames Feist         BMCWEB_LOG_ERROR << "Empty Zones";
529b6baeaa4SJames Feist         messages::propertyValueFormatError(response->res,
530b6baeaa4SJames Feist                                            nlohmann::json::array(), "Zones");
531b6baeaa4SJames Feist         return false;
532b6baeaa4SJames Feist     }
5335f2caaefSJames Feist     for (auto& odata : config)
5345f2caaefSJames Feist     {
5355f2caaefSJames Feist         std::string path;
5365f2caaefSJames Feist         if (!redfish::json_util::readJson(odata, response->res, "@odata.id",
5375f2caaefSJames Feist                                           path))
5385f2caaefSJames Feist         {
5395f2caaefSJames Feist             return false;
5405f2caaefSJames Feist         }
5415f2caaefSJames Feist         std::string input;
54261adbda3SJames Feist 
54361adbda3SJames Feist         // 8 below comes from
54461adbda3SJames Feist         // /redfish/v1/Managers/bmc#/Oem/OpenBmc/Fan/FanZones/Left
54561adbda3SJames Feist         //     0    1     2      3    4    5      6     7      8
54661adbda3SJames Feist         if (!dbus::utility::getNthStringFromPath(path, 8, input))
5475f2caaefSJames Feist         {
5485f2caaefSJames Feist             BMCWEB_LOG_ERROR << "Got invalid path " << path;
5495f2caaefSJames Feist             BMCWEB_LOG_ERROR << "Illegal Type Zones";
5505f2caaefSJames Feist             messages::propertyValueFormatError(response->res, odata.dump(),
5515f2caaefSJames Feist                                                "Zones");
5525f2caaefSJames Feist             return false;
5535f2caaefSJames Feist         }
5545f2caaefSJames Feist         boost::replace_all(input, "_", " ");
5555f2caaefSJames Feist         zones.emplace_back(std::move(input));
5565f2caaefSJames Feist     }
5575f2caaefSJames Feist     return true;
5585f2caaefSJames Feist }
5595f2caaefSJames Feist 
56073df0db0SJames Feist static const dbus::utility::ManagedItem*
56173df0db0SJames Feist     findChassis(const dbus::utility::ManagedObjectType& managedObj,
562b6baeaa4SJames Feist                 const std::string& value, std::string& chassis)
563b6baeaa4SJames Feist {
564b6baeaa4SJames Feist     BMCWEB_LOG_DEBUG << "Find Chassis: " << value << "\n";
565b6baeaa4SJames Feist 
566b6baeaa4SJames Feist     std::string escaped = boost::replace_all_copy(value, " ", "_");
567b6baeaa4SJames Feist     escaped = "/" + escaped;
568b6baeaa4SJames Feist     auto it = std::find_if(
569b6baeaa4SJames Feist         managedObj.begin(), managedObj.end(), [&escaped](const auto& obj) {
570b6baeaa4SJames Feist             if (boost::algorithm::ends_with(obj.first.str, escaped))
571b6baeaa4SJames Feist             {
572b6baeaa4SJames Feist                 BMCWEB_LOG_DEBUG << "Matched " << obj.first.str << "\n";
573b6baeaa4SJames Feist                 return true;
574b6baeaa4SJames Feist             }
575b6baeaa4SJames Feist             return false;
576b6baeaa4SJames Feist         });
577b6baeaa4SJames Feist 
578b6baeaa4SJames Feist     if (it == managedObj.end())
579b6baeaa4SJames Feist     {
58073df0db0SJames Feist         return nullptr;
581b6baeaa4SJames Feist     }
582b6baeaa4SJames Feist     // 5 comes from <chassis-name> being the 5th element
583b6baeaa4SJames Feist     // /xyz/openbmc_project/inventory/system/chassis/<chassis-name>
58473df0db0SJames Feist     if (dbus::utility::getNthStringFromPath(it->first.str, 5, chassis))
58573df0db0SJames Feist     {
58673df0db0SJames Feist         return &(*it);
58773df0db0SJames Feist     }
58873df0db0SJames Feist 
58973df0db0SJames Feist     return nullptr;
590b6baeaa4SJames Feist }
591b6baeaa4SJames Feist 
59283ff9ab6SJames Feist static CreatePIDRet createPidInterface(
59383ff9ab6SJames Feist     const std::shared_ptr<AsyncResp>& response, const std::string& type,
594b6baeaa4SJames Feist     nlohmann::json::iterator it, const std::string& path,
59583ff9ab6SJames Feist     const dbus::utility::ManagedObjectType& managedObj, bool createNewObject,
59683ff9ab6SJames Feist     boost::container::flat_map<std::string, dbus::utility::DbusVariantType>&
59783ff9ab6SJames Feist         output,
59873df0db0SJames Feist     std::string& chassis, const std::string& profile)
59983ff9ab6SJames Feist {
60083ff9ab6SJames Feist 
6015f2caaefSJames Feist     // common deleter
602b6baeaa4SJames Feist     if (it.value() == nullptr)
6035f2caaefSJames Feist     {
6045f2caaefSJames Feist         std::string iface;
6055f2caaefSJames Feist         if (type == "PidControllers" || type == "FanControllers")
6065f2caaefSJames Feist         {
6075f2caaefSJames Feist             iface = pidConfigurationIface;
6085f2caaefSJames Feist         }
6095f2caaefSJames Feist         else if (type == "FanZones")
6105f2caaefSJames Feist         {
6115f2caaefSJames Feist             iface = pidZoneConfigurationIface;
6125f2caaefSJames Feist         }
6135f2caaefSJames Feist         else if (type == "StepwiseControllers")
6145f2caaefSJames Feist         {
6155f2caaefSJames Feist             iface = stepwiseConfigurationIface;
6165f2caaefSJames Feist         }
6175f2caaefSJames Feist         else
6185f2caaefSJames Feist         {
6195f2caaefSJames Feist             BMCWEB_LOG_ERROR << "Line:" << __LINE__ << ", Illegal Type "
6205f2caaefSJames Feist                              << type;
6215f2caaefSJames Feist             messages::propertyUnknown(response->res, type);
6225f2caaefSJames Feist             return CreatePIDRet::fail;
6235f2caaefSJames Feist         }
6245f2caaefSJames Feist         // delete interface
6255f2caaefSJames Feist         crow::connections::systemBus->async_method_call(
6265f2caaefSJames Feist             [response, path](const boost::system::error_code ec) {
6275f2caaefSJames Feist                 if (ec)
6285f2caaefSJames Feist                 {
6295f2caaefSJames Feist                     BMCWEB_LOG_ERROR << "Error patching " << path << ": " << ec;
6305f2caaefSJames Feist                     messages::internalError(response->res);
631b6baeaa4SJames Feist                     return;
6325f2caaefSJames Feist                 }
633b6baeaa4SJames Feist                 messages::success(response->res);
6345f2caaefSJames Feist             },
6355f2caaefSJames Feist             "xyz.openbmc_project.EntityManager", path, iface, "Delete");
6365f2caaefSJames Feist         return CreatePIDRet::del;
6375f2caaefSJames Feist     }
6385f2caaefSJames Feist 
63973df0db0SJames Feist     const dbus::utility::ManagedItem* managedItem = nullptr;
640b6baeaa4SJames Feist     if (!createNewObject)
641b6baeaa4SJames Feist     {
642b6baeaa4SJames Feist         // if we aren't creating a new object, we should be able to find it on
643b6baeaa4SJames Feist         // d-bus
64473df0db0SJames Feist         managedItem = findChassis(managedObj, it.key(), chassis);
64573df0db0SJames Feist         if (managedItem == nullptr)
646b6baeaa4SJames Feist         {
647b6baeaa4SJames Feist             BMCWEB_LOG_ERROR << "Failed to get chassis from config patch";
648b6baeaa4SJames Feist             messages::invalidObject(response->res, it.key());
649b6baeaa4SJames Feist             return CreatePIDRet::fail;
650b6baeaa4SJames Feist         }
651b6baeaa4SJames Feist     }
652b6baeaa4SJames Feist 
65373df0db0SJames Feist     if (profile.size() &&
65473df0db0SJames Feist         (type == "PidControllers" || type == "FanControllers" ||
65573df0db0SJames Feist          type == "StepwiseControllers"))
65673df0db0SJames Feist     {
65773df0db0SJames Feist         if (managedItem == nullptr)
65873df0db0SJames Feist         {
65973df0db0SJames Feist             output["Profiles"] = std::vector<std::string>{profile};
66073df0db0SJames Feist         }
66173df0db0SJames Feist         else
66273df0db0SJames Feist         {
66373df0db0SJames Feist             std::string interface;
66473df0db0SJames Feist             if (type == "StepwiseControllers")
66573df0db0SJames Feist             {
66673df0db0SJames Feist                 interface = stepwiseConfigurationIface;
66773df0db0SJames Feist             }
66873df0db0SJames Feist             else
66973df0db0SJames Feist             {
67073df0db0SJames Feist                 interface = pidConfigurationIface;
67173df0db0SJames Feist             }
67273df0db0SJames Feist             auto findConfig = managedItem->second.find(interface);
67373df0db0SJames Feist             if (findConfig == managedItem->second.end())
67473df0db0SJames Feist             {
67573df0db0SJames Feist                 BMCWEB_LOG_ERROR
67673df0db0SJames Feist                     << "Failed to find interface in managed object";
67773df0db0SJames Feist                 messages::internalError(response->res);
67873df0db0SJames Feist                 return CreatePIDRet::fail;
67973df0db0SJames Feist             }
68073df0db0SJames Feist             auto findProfiles = findConfig->second.find("Profiles");
68173df0db0SJames Feist             if (findProfiles != findConfig->second.end())
68273df0db0SJames Feist             {
68373df0db0SJames Feist                 const std::vector<std::string>* curProfiles =
68473df0db0SJames Feist                     std::get_if<std::vector<std::string>>(
68573df0db0SJames Feist                         &(findProfiles->second));
68673df0db0SJames Feist                 if (curProfiles == nullptr)
68773df0db0SJames Feist                 {
68873df0db0SJames Feist                     BMCWEB_LOG_ERROR << "Illegal profiles in managed object";
68973df0db0SJames Feist                     messages::internalError(response->res);
69073df0db0SJames Feist                     return CreatePIDRet::fail;
69173df0db0SJames Feist                 }
69273df0db0SJames Feist                 if (std::find(curProfiles->begin(), curProfiles->end(),
69373df0db0SJames Feist                               profile) == curProfiles->end())
69473df0db0SJames Feist                 {
69573df0db0SJames Feist                     std::vector<std::string> newProfiles = *curProfiles;
69673df0db0SJames Feist                     newProfiles.push_back(profile);
69773df0db0SJames Feist                     output["Profiles"] = newProfiles;
69873df0db0SJames Feist                 }
69973df0db0SJames Feist             }
70073df0db0SJames Feist         }
70173df0db0SJames Feist     }
70273df0db0SJames Feist 
70383ff9ab6SJames Feist     if (type == "PidControllers" || type == "FanControllers")
70483ff9ab6SJames Feist     {
70583ff9ab6SJames Feist         if (createNewObject)
70683ff9ab6SJames Feist         {
70783ff9ab6SJames Feist             output["Class"] = type == "PidControllers" ? std::string("temp")
70883ff9ab6SJames Feist                                                        : std::string("fan");
70983ff9ab6SJames Feist             output["Type"] = std::string("Pid");
71083ff9ab6SJames Feist         }
7115f2caaefSJames Feist 
7125f2caaefSJames Feist         std::optional<std::vector<nlohmann::json>> zones;
7135f2caaefSJames Feist         std::optional<std::vector<std::string>> inputs;
7145f2caaefSJames Feist         std::optional<std::vector<std::string>> outputs;
7155f2caaefSJames Feist         std::map<std::string, std::optional<double>> doubles;
7165f2caaefSJames Feist         if (!redfish::json_util::readJson(
717b6baeaa4SJames Feist                 it.value(), response->res, "Inputs", inputs, "Outputs", outputs,
7185f2caaefSJames Feist                 "Zones", zones, "FFGainCoefficient",
7195f2caaefSJames Feist                 doubles["FFGainCoefficient"], "FFOffCoefficient",
7205f2caaefSJames Feist                 doubles["FFOffCoefficient"], "ICoefficient",
7215f2caaefSJames Feist                 doubles["ICoefficient"], "ILimitMax", doubles["ILimitMax"],
7225f2caaefSJames Feist                 "ILimitMin", doubles["ILimitMin"], "OutLimitMax",
7235f2caaefSJames Feist                 doubles["OutLimitMax"], "OutLimitMin", doubles["OutLimitMin"],
7245f2caaefSJames Feist                 "PCoefficient", doubles["PCoefficient"], "SetPoint",
7255f2caaefSJames Feist                 doubles["SetPoint"], "SlewNeg", doubles["SlewNeg"], "SlewPos",
726aad1a257SJames Feist                 doubles["SlewPos"], "PositiveHysteresis",
727aad1a257SJames Feist                 doubles["PositiveHysteresis"], "NegativeHysteresis",
728aad1a257SJames Feist                 doubles["NegativeHysteresis"]))
72983ff9ab6SJames Feist         {
7305f2caaefSJames Feist             BMCWEB_LOG_ERROR << "Line:" << __LINE__ << ", Illegal Property "
731b6baeaa4SJames Feist                              << it.value().dump();
7325f2caaefSJames Feist             return CreatePIDRet::fail;
73383ff9ab6SJames Feist         }
7345f2caaefSJames Feist         if (zones)
7355f2caaefSJames Feist         {
7365f2caaefSJames Feist             std::vector<std::string> zonesStr;
7375f2caaefSJames Feist             if (!getZonesFromJsonReq(response, *zones, zonesStr))
7385f2caaefSJames Feist             {
7395f2caaefSJames Feist                 BMCWEB_LOG_ERROR << "Line:" << __LINE__ << ", Illegal Zones";
7405f2caaefSJames Feist                 return CreatePIDRet::fail;
7415f2caaefSJames Feist             }
742b6baeaa4SJames Feist             if (chassis.empty() &&
743b6baeaa4SJames Feist                 !findChassis(managedObj, zonesStr[0], chassis))
744b6baeaa4SJames Feist             {
745b6baeaa4SJames Feist                 BMCWEB_LOG_ERROR << "Failed to get chassis from config patch";
746b6baeaa4SJames Feist                 messages::invalidObject(response->res, it.key());
747b6baeaa4SJames Feist                 return CreatePIDRet::fail;
748b6baeaa4SJames Feist             }
749b6baeaa4SJames Feist 
7505f2caaefSJames Feist             output["Zones"] = std::move(zonesStr);
7515f2caaefSJames Feist         }
7525f2caaefSJames Feist         if (inputs || outputs)
7535f2caaefSJames Feist         {
7545f2caaefSJames Feist             std::array<std::optional<std::vector<std::string>>*, 2> containers =
7555f2caaefSJames Feist                 {&inputs, &outputs};
7565f2caaefSJames Feist             size_t index = 0;
7575f2caaefSJames Feist             for (const auto& containerPtr : containers)
7585f2caaefSJames Feist             {
7595f2caaefSJames Feist                 std::optional<std::vector<std::string>>& container =
7605f2caaefSJames Feist                     *containerPtr;
7615f2caaefSJames Feist                 if (!container)
7625f2caaefSJames Feist                 {
7635f2caaefSJames Feist                     index++;
7645f2caaefSJames Feist                     continue;
76583ff9ab6SJames Feist                 }
76683ff9ab6SJames Feist 
7675f2caaefSJames Feist                 for (std::string& value : *container)
76883ff9ab6SJames Feist                 {
7695f2caaefSJames Feist                     boost::replace_all(value, "_", " ");
77083ff9ab6SJames Feist                 }
7715f2caaefSJames Feist                 std::string key;
7725f2caaefSJames Feist                 if (index == 0)
7735f2caaefSJames Feist                 {
7745f2caaefSJames Feist                     key = "Inputs";
7755f2caaefSJames Feist                 }
7765f2caaefSJames Feist                 else
7775f2caaefSJames Feist                 {
7785f2caaefSJames Feist                     key = "Outputs";
7795f2caaefSJames Feist                 }
7805f2caaefSJames Feist                 output[key] = *container;
7815f2caaefSJames Feist                 index++;
7825f2caaefSJames Feist             }
78383ff9ab6SJames Feist         }
78483ff9ab6SJames Feist 
78583ff9ab6SJames Feist         // doubles
7865f2caaefSJames Feist         for (const auto& pairs : doubles)
78783ff9ab6SJames Feist         {
7885f2caaefSJames Feist             if (!pairs.second)
78983ff9ab6SJames Feist             {
7905f2caaefSJames Feist                 continue;
79183ff9ab6SJames Feist             }
7925f2caaefSJames Feist             BMCWEB_LOG_DEBUG << pairs.first << " = " << *pairs.second;
7935f2caaefSJames Feist             output[pairs.first] = *(pairs.second);
7945f2caaefSJames Feist         }
79583ff9ab6SJames Feist     }
79683ff9ab6SJames Feist 
79783ff9ab6SJames Feist     else if (type == "FanZones")
79883ff9ab6SJames Feist     {
79983ff9ab6SJames Feist         output["Type"] = std::string("Pid.Zone");
80083ff9ab6SJames Feist 
8015f2caaefSJames Feist         std::optional<nlohmann::json> chassisContainer;
8025f2caaefSJames Feist         std::optional<double> failSafePercent;
803d3ec07f8SJames Feist         std::optional<double> minThermalOutput;
804b6baeaa4SJames Feist         if (!redfish::json_util::readJson(it.value(), response->res, "Chassis",
8055f2caaefSJames Feist                                           chassisContainer, "FailSafePercent",
806d3ec07f8SJames Feist                                           failSafePercent, "MinThermalOutput",
807d3ec07f8SJames Feist                                           minThermalOutput))
80883ff9ab6SJames Feist         {
8095f2caaefSJames Feist             BMCWEB_LOG_ERROR << "Line:" << __LINE__ << ", Illegal Property "
810b6baeaa4SJames Feist                              << it.value().dump();
81183ff9ab6SJames Feist             return CreatePIDRet::fail;
81283ff9ab6SJames Feist         }
8135f2caaefSJames Feist 
8145f2caaefSJames Feist         if (chassisContainer)
81583ff9ab6SJames Feist         {
8165f2caaefSJames Feist 
8175f2caaefSJames Feist             std::string chassisId;
8185f2caaefSJames Feist             if (!redfish::json_util::readJson(*chassisContainer, response->res,
8195f2caaefSJames Feist                                               "@odata.id", chassisId))
8205f2caaefSJames Feist             {
8215f2caaefSJames Feist                 BMCWEB_LOG_ERROR << "Line:" << __LINE__ << ", Illegal Property "
8225f2caaefSJames Feist                                  << chassisContainer->dump();
82383ff9ab6SJames Feist                 return CreatePIDRet::fail;
82483ff9ab6SJames Feist             }
82583ff9ab6SJames Feist 
82683ff9ab6SJames Feist             // /refish/v1/chassis/chassis_name/
8275f2caaefSJames Feist             if (!dbus::utility::getNthStringFromPath(chassisId, 3, chassis))
82883ff9ab6SJames Feist             {
8295f2caaefSJames Feist                 BMCWEB_LOG_ERROR << "Got invalid path " << chassisId;
8305f2caaefSJames Feist                 messages::invalidObject(response->res, chassisId);
83183ff9ab6SJames Feist                 return CreatePIDRet::fail;
83283ff9ab6SJames Feist             }
83383ff9ab6SJames Feist         }
834d3ec07f8SJames Feist         if (minThermalOutput)
83583ff9ab6SJames Feist         {
836d3ec07f8SJames Feist             output["MinThermalOutput"] = *minThermalOutput;
8375f2caaefSJames Feist         }
8385f2caaefSJames Feist         if (failSafePercent)
83983ff9ab6SJames Feist         {
8405f2caaefSJames Feist             output["FailSafePercent"] = *failSafePercent;
8415f2caaefSJames Feist         }
8425f2caaefSJames Feist     }
8435f2caaefSJames Feist     else if (type == "StepwiseControllers")
8445f2caaefSJames Feist     {
8455f2caaefSJames Feist         output["Type"] = std::string("Stepwise");
8465f2caaefSJames Feist 
8475f2caaefSJames Feist         std::optional<std::vector<nlohmann::json>> zones;
8485f2caaefSJames Feist         std::optional<std::vector<nlohmann::json>> steps;
8495f2caaefSJames Feist         std::optional<std::vector<std::string>> inputs;
8505f2caaefSJames Feist         std::optional<double> positiveHysteresis;
8515f2caaefSJames Feist         std::optional<double> negativeHysteresis;
852c33a90ecSJames Feist         std::optional<std::string> direction; // upper clipping curve vs lower
8535f2caaefSJames Feist         if (!redfish::json_util::readJson(
854b6baeaa4SJames Feist                 it.value(), response->res, "Zones", zones, "Steps", steps,
855b6baeaa4SJames Feist                 "Inputs", inputs, "PositiveHysteresis", positiveHysteresis,
856c33a90ecSJames Feist                 "NegativeHysteresis", negativeHysteresis, "Direction",
857c33a90ecSJames Feist                 direction))
8585f2caaefSJames Feist         {
8595f2caaefSJames Feist             BMCWEB_LOG_ERROR << "Line:" << __LINE__ << ", Illegal Property "
860b6baeaa4SJames Feist                              << it.value().dump();
86183ff9ab6SJames Feist             return CreatePIDRet::fail;
86283ff9ab6SJames Feist         }
8635f2caaefSJames Feist 
8645f2caaefSJames Feist         if (zones)
86583ff9ab6SJames Feist         {
866b6baeaa4SJames Feist             std::vector<std::string> zonesStrs;
867b6baeaa4SJames Feist             if (!getZonesFromJsonReq(response, *zones, zonesStrs))
8685f2caaefSJames Feist             {
8695f2caaefSJames Feist                 BMCWEB_LOG_ERROR << "Line:" << __LINE__ << ", Illegal Zones";
87083ff9ab6SJames Feist                 return CreatePIDRet::fail;
87183ff9ab6SJames Feist             }
872b6baeaa4SJames Feist             if (chassis.empty() &&
873b6baeaa4SJames Feist                 !findChassis(managedObj, zonesStrs[0], chassis))
874b6baeaa4SJames Feist             {
875b6baeaa4SJames Feist                 BMCWEB_LOG_ERROR << "Failed to get chassis from config patch";
876b6baeaa4SJames Feist                 messages::invalidObject(response->res, it.key());
877b6baeaa4SJames Feist                 return CreatePIDRet::fail;
878b6baeaa4SJames Feist             }
879b6baeaa4SJames Feist             output["Zones"] = std::move(zonesStrs);
8805f2caaefSJames Feist         }
8815f2caaefSJames Feist         if (steps)
8825f2caaefSJames Feist         {
8835f2caaefSJames Feist             std::vector<double> readings;
8845f2caaefSJames Feist             std::vector<double> outputs;
8855f2caaefSJames Feist             for (auto& step : *steps)
8865f2caaefSJames Feist             {
8875f2caaefSJames Feist                 double target;
888b01bf299SEd Tanous                 double output;
8895f2caaefSJames Feist 
8905f2caaefSJames Feist                 if (!redfish::json_util::readJson(step, response->res, "Target",
891b01bf299SEd Tanous                                                   target, "Output", output))
8925f2caaefSJames Feist                 {
8935f2caaefSJames Feist                     BMCWEB_LOG_ERROR << "Line:" << __LINE__
894b6baeaa4SJames Feist                                      << ", Illegal Property "
895b6baeaa4SJames Feist                                      << it.value().dump();
8965f2caaefSJames Feist                     return CreatePIDRet::fail;
8975f2caaefSJames Feist                 }
8985f2caaefSJames Feist                 readings.emplace_back(target);
899b01bf299SEd Tanous                 outputs.emplace_back(output);
9005f2caaefSJames Feist             }
9015f2caaefSJames Feist             output["Reading"] = std::move(readings);
9025f2caaefSJames Feist             output["Output"] = std::move(outputs);
9035f2caaefSJames Feist         }
9045f2caaefSJames Feist         if (inputs)
9055f2caaefSJames Feist         {
9065f2caaefSJames Feist             for (std::string& value : *inputs)
9075f2caaefSJames Feist             {
9085f2caaefSJames Feist                 boost::replace_all(value, "_", " ");
9095f2caaefSJames Feist             }
9105f2caaefSJames Feist             output["Inputs"] = std::move(*inputs);
9115f2caaefSJames Feist         }
9125f2caaefSJames Feist         if (negativeHysteresis)
9135f2caaefSJames Feist         {
9145f2caaefSJames Feist             output["NegativeHysteresis"] = *negativeHysteresis;
9155f2caaefSJames Feist         }
9165f2caaefSJames Feist         if (positiveHysteresis)
9175f2caaefSJames Feist         {
9185f2caaefSJames Feist             output["PositiveHysteresis"] = *positiveHysteresis;
91983ff9ab6SJames Feist         }
920c33a90ecSJames Feist         if (direction)
921c33a90ecSJames Feist         {
922c33a90ecSJames Feist             constexpr const std::array<const char*, 2> allowedDirections = {
923c33a90ecSJames Feist                 "Ceiling", "Floor"};
924c33a90ecSJames Feist             if (std::find(allowedDirections.begin(), allowedDirections.end(),
925c33a90ecSJames Feist                           *direction) == allowedDirections.end())
926c33a90ecSJames Feist             {
927c33a90ecSJames Feist                 messages::propertyValueTypeError(response->res, "Direction",
928c33a90ecSJames Feist                                                  *direction);
929c33a90ecSJames Feist                 return CreatePIDRet::fail;
930c33a90ecSJames Feist             }
931c33a90ecSJames Feist             output["Class"] = *direction;
932c33a90ecSJames Feist         }
93383ff9ab6SJames Feist     }
93483ff9ab6SJames Feist     else
93583ff9ab6SJames Feist     {
9365f2caaefSJames Feist         BMCWEB_LOG_ERROR << "Line:" << __LINE__ << ", Illegal Type " << type;
93735a62c7cSJason M. Bills         messages::propertyUnknown(response->res, type);
93883ff9ab6SJames Feist         return CreatePIDRet::fail;
93983ff9ab6SJames Feist     }
94083ff9ab6SJames Feist     return CreatePIDRet::patch;
94183ff9ab6SJames Feist }
94273df0db0SJames Feist struct GetPIDValues : std::enable_shared_from_this<GetPIDValues>
94373df0db0SJames Feist {
94483ff9ab6SJames Feist 
94573df0db0SJames Feist     GetPIDValues(const std::shared_ptr<AsyncResp>& asyncResp) :
94673df0db0SJames Feist         asyncResp(asyncResp)
94773df0db0SJames Feist 
9481abe55efSEd Tanous     {
9499c310685SBorawski.Lukasz     }
9509c310685SBorawski.Lukasz 
95173df0db0SJames Feist     void run()
9525b4aa86bSJames Feist     {
95373df0db0SJames Feist         std::shared_ptr<GetPIDValues> self = shared_from_this();
95473df0db0SJames Feist 
95573df0db0SJames Feist         // get all configurations
9565b4aa86bSJames Feist         crow::connections::systemBus->async_method_call(
95773df0db0SJames Feist             [self](const boost::system::error_code ec,
9585b4aa86bSJames Feist                    const crow::openbmc_mapper::GetSubTreeType& subtree) {
9595b4aa86bSJames Feist                 if (ec)
9605b4aa86bSJames Feist                 {
9615b4aa86bSJames Feist                     BMCWEB_LOG_ERROR << ec;
96273df0db0SJames Feist                     messages::internalError(self->asyncResp->res);
96373df0db0SJames Feist                     return;
96473df0db0SJames Feist                 }
96573df0db0SJames Feist                 self->subtree = subtree;
96673df0db0SJames Feist             },
96773df0db0SJames Feist             "xyz.openbmc_project.ObjectMapper",
96873df0db0SJames Feist             "/xyz/openbmc_project/object_mapper",
96973df0db0SJames Feist             "xyz.openbmc_project.ObjectMapper", "GetSubTree", "/", 0,
97073df0db0SJames Feist             std::array<const char*, 4>{
97173df0db0SJames Feist                 pidConfigurationIface, pidZoneConfigurationIface,
97273df0db0SJames Feist                 objectManagerIface, stepwiseConfigurationIface});
97373df0db0SJames Feist 
97473df0db0SJames Feist         // at the same time get the selected profile
97573df0db0SJames Feist         crow::connections::systemBus->async_method_call(
97673df0db0SJames Feist             [self](const boost::system::error_code ec,
97773df0db0SJames Feist                    const crow::openbmc_mapper::GetSubTreeType& subtree) {
97873df0db0SJames Feist                 if (ec || subtree.empty())
97973df0db0SJames Feist                 {
98073df0db0SJames Feist                     return;
98173df0db0SJames Feist                 }
98273df0db0SJames Feist                 if (subtree[0].second.size() != 1)
98373df0db0SJames Feist                 {
98473df0db0SJames Feist                     // invalid mapper response, should never happen
98573df0db0SJames Feist                     BMCWEB_LOG_ERROR << "GetPIDValues: Mapper Error";
98673df0db0SJames Feist                     messages::internalError(self->asyncResp->res);
9875b4aa86bSJames Feist                     return;
9885b4aa86bSJames Feist                 }
9895b4aa86bSJames Feist 
99073df0db0SJames Feist                 const std::string& path = subtree[0].first;
99173df0db0SJames Feist                 const std::string& owner = subtree[0].second[0].first;
99273df0db0SJames Feist                 crow::connections::systemBus->async_method_call(
99373df0db0SJames Feist                     [path, owner, self](
99473df0db0SJames Feist                         const boost::system::error_code ec,
99573df0db0SJames Feist                         const boost::container::flat_map<
99673df0db0SJames Feist                             std::string, std::variant<std::vector<std::string>,
99773df0db0SJames Feist                                                       std::string>>& resp) {
99873df0db0SJames Feist                         if (ec)
99973df0db0SJames Feist                         {
100073df0db0SJames Feist                             BMCWEB_LOG_ERROR << "GetPIDValues: Can't get "
100173df0db0SJames Feist                                                 "thermalModeIface "
100273df0db0SJames Feist                                              << path;
100373df0db0SJames Feist                             messages::internalError(self->asyncResp->res);
100473df0db0SJames Feist                             return;
100573df0db0SJames Feist                         }
100673df0db0SJames Feist                         const std::string* current;
100773df0db0SJames Feist                         const std::vector<std::string>* supported;
100873df0db0SJames Feist                         for (auto& [key, value] : resp)
100973df0db0SJames Feist                         {
101073df0db0SJames Feist                             if (key == "Current")
101173df0db0SJames Feist                             {
101273df0db0SJames Feist                                 current = std::get_if<std::string>(&value);
101373df0db0SJames Feist                                 if (current == nullptr)
101473df0db0SJames Feist                                 {
101573df0db0SJames Feist                                     BMCWEB_LOG_ERROR
101673df0db0SJames Feist                                         << "GetPIDValues: thermal mode "
101773df0db0SJames Feist                                            "iface invalid "
101873df0db0SJames Feist                                         << path;
101973df0db0SJames Feist                                     messages::internalError(
102073df0db0SJames Feist                                         self->asyncResp->res);
102173df0db0SJames Feist                                     return;
102273df0db0SJames Feist                                 }
102373df0db0SJames Feist                             }
102473df0db0SJames Feist                             if (key == "Supported")
102573df0db0SJames Feist                             {
102673df0db0SJames Feist                                 supported =
102773df0db0SJames Feist                                     std::get_if<std::vector<std::string>>(
102873df0db0SJames Feist                                         &value);
102973df0db0SJames Feist                                 if (supported == nullptr)
103073df0db0SJames Feist                                 {
103173df0db0SJames Feist                                     BMCWEB_LOG_ERROR
103273df0db0SJames Feist                                         << "GetPIDValues: thermal mode "
103373df0db0SJames Feist                                            "iface invalid"
103473df0db0SJames Feist                                         << path;
103573df0db0SJames Feist                                     messages::internalError(
103673df0db0SJames Feist                                         self->asyncResp->res);
103773df0db0SJames Feist                                     return;
103873df0db0SJames Feist                                 }
103973df0db0SJames Feist                             }
104073df0db0SJames Feist                         }
104173df0db0SJames Feist                         if (current == nullptr || supported == nullptr)
104273df0db0SJames Feist                         {
104373df0db0SJames Feist                             BMCWEB_LOG_ERROR << "GetPIDValues: thermal mode "
104473df0db0SJames Feist                                                 "iface invalid "
104573df0db0SJames Feist                                              << path;
104673df0db0SJames Feist                             messages::internalError(self->asyncResp->res);
104773df0db0SJames Feist                             return;
104873df0db0SJames Feist                         }
104973df0db0SJames Feist                         self->currentProfile = *current;
105073df0db0SJames Feist                         self->supportedProfiles = *supported;
105173df0db0SJames Feist                     },
105273df0db0SJames Feist                     owner, path, "org.freedesktop.DBus.Properties", "GetAll",
105373df0db0SJames Feist                     thermalModeIface);
105473df0db0SJames Feist             },
105573df0db0SJames Feist             "xyz.openbmc_project.ObjectMapper",
105673df0db0SJames Feist             "/xyz/openbmc_project/object_mapper",
105773df0db0SJames Feist             "xyz.openbmc_project.ObjectMapper", "GetSubTree", "/", 0,
105873df0db0SJames Feist             std::array<const char*, 1>{thermalModeIface});
105973df0db0SJames Feist     }
106073df0db0SJames Feist 
106173df0db0SJames Feist     ~GetPIDValues()
106273df0db0SJames Feist     {
106373df0db0SJames Feist         if (asyncResp->res.result() != boost::beast::http::status::ok)
106473df0db0SJames Feist         {
106573df0db0SJames Feist             return;
106673df0db0SJames Feist         }
10675b4aa86bSJames Feist         // create map of <connection, path to objMgr>>
106873df0db0SJames Feist         boost::container::flat_map<std::string, std::string> objectMgrPaths;
10696bce33bcSJames Feist         boost::container::flat_set<std::string> calledConnections;
10705b4aa86bSJames Feist         for (const auto& pathGroup : subtree)
10715b4aa86bSJames Feist         {
10725b4aa86bSJames Feist             for (const auto& connectionGroup : pathGroup.second)
10735b4aa86bSJames Feist             {
10746bce33bcSJames Feist                 auto findConnection =
10756bce33bcSJames Feist                     calledConnections.find(connectionGroup.first);
10766bce33bcSJames Feist                 if (findConnection != calledConnections.end())
10776bce33bcSJames Feist                 {
10786bce33bcSJames Feist                     break;
10796bce33bcSJames Feist                 }
108073df0db0SJames Feist                 for (const std::string& interface : connectionGroup.second)
10815b4aa86bSJames Feist                 {
10825b4aa86bSJames Feist                     if (interface == objectManagerIface)
10835b4aa86bSJames Feist                     {
108473df0db0SJames Feist                         objectMgrPaths[connectionGroup.first] = pathGroup.first;
10855b4aa86bSJames Feist                     }
10865b4aa86bSJames Feist                     // this list is alphabetical, so we
10875b4aa86bSJames Feist                     // should have found the objMgr by now
10885b4aa86bSJames Feist                     if (interface == pidConfigurationIface ||
1089b7a08d04SJames Feist                         interface == pidZoneConfigurationIface ||
1090b7a08d04SJames Feist                         interface == stepwiseConfigurationIface)
10915b4aa86bSJames Feist                     {
10925b4aa86bSJames Feist                         auto findObjMgr =
10935b4aa86bSJames Feist                             objectMgrPaths.find(connectionGroup.first);
10945b4aa86bSJames Feist                         if (findObjMgr == objectMgrPaths.end())
10955b4aa86bSJames Feist                         {
10965b4aa86bSJames Feist                             BMCWEB_LOG_DEBUG << connectionGroup.first
10975b4aa86bSJames Feist                                              << "Has no Object Manager";
10985b4aa86bSJames Feist                             continue;
10995b4aa86bSJames Feist                         }
11006bce33bcSJames Feist 
11016bce33bcSJames Feist                         calledConnections.insert(connectionGroup.first);
11026bce33bcSJames Feist 
110373df0db0SJames Feist                         asyncPopulatePid(findObjMgr->first, findObjMgr->second,
110473df0db0SJames Feist                                          currentProfile, supportedProfiles,
110573df0db0SJames Feist                                          asyncResp);
11065b4aa86bSJames Feist                         break;
11075b4aa86bSJames Feist                     }
11085b4aa86bSJames Feist                 }
11095b4aa86bSJames Feist             }
11105b4aa86bSJames Feist         }
111173df0db0SJames Feist     }
111273df0db0SJames Feist 
111373df0db0SJames Feist     std::vector<std::string> supportedProfiles;
111473df0db0SJames Feist     std::string currentProfile;
111573df0db0SJames Feist     crow::openbmc_mapper::GetSubTreeType subtree;
111673df0db0SJames Feist     std::shared_ptr<AsyncResp> asyncResp;
111773df0db0SJames Feist };
111873df0db0SJames Feist 
111973df0db0SJames Feist struct SetPIDValues : std::enable_shared_from_this<SetPIDValues>
112073df0db0SJames Feist {
112173df0db0SJames Feist 
112273df0db0SJames Feist     SetPIDValues(const std::shared_ptr<AsyncResp>& asyncResp,
112373df0db0SJames Feist                  nlohmann::json& data) :
112473df0db0SJames Feist         asyncResp(asyncResp)
112573df0db0SJames Feist     {
112673df0db0SJames Feist 
112773df0db0SJames Feist         std::optional<nlohmann::json> pidControllers;
112873df0db0SJames Feist         std::optional<nlohmann::json> fanControllers;
112973df0db0SJames Feist         std::optional<nlohmann::json> fanZones;
113073df0db0SJames Feist         std::optional<nlohmann::json> stepwiseControllers;
113173df0db0SJames Feist 
113273df0db0SJames Feist         if (!redfish::json_util::readJson(
113373df0db0SJames Feist                 data, asyncResp->res, "PidControllers", pidControllers,
113473df0db0SJames Feist                 "FanControllers", fanControllers, "FanZones", fanZones,
113573df0db0SJames Feist                 "StepwiseControllers", stepwiseControllers, "Profile", profile))
113673df0db0SJames Feist         {
113773df0db0SJames Feist             BMCWEB_LOG_ERROR << "Line:" << __LINE__ << ", Illegal Property "
113873df0db0SJames Feist                              << data.dump();
113973df0db0SJames Feist             return;
114073df0db0SJames Feist         }
114173df0db0SJames Feist         configuration.emplace_back("PidControllers", std::move(pidControllers));
114273df0db0SJames Feist         configuration.emplace_back("FanControllers", std::move(fanControllers));
114373df0db0SJames Feist         configuration.emplace_back("FanZones", std::move(fanZones));
114473df0db0SJames Feist         configuration.emplace_back("StepwiseControllers",
114573df0db0SJames Feist                                    std::move(stepwiseControllers));
114673df0db0SJames Feist     }
114773df0db0SJames Feist     void run()
114873df0db0SJames Feist     {
114973df0db0SJames Feist         if (asyncResp->res.result() != boost::beast::http::status::ok)
115073df0db0SJames Feist         {
115173df0db0SJames Feist             return;
115273df0db0SJames Feist         }
115373df0db0SJames Feist 
115473df0db0SJames Feist         std::shared_ptr<SetPIDValues> self = shared_from_this();
115573df0db0SJames Feist 
115673df0db0SJames Feist         // todo(james): might make sense to do a mapper call here if this
115773df0db0SJames Feist         // interface gets more traction
115873df0db0SJames Feist         crow::connections::systemBus->async_method_call(
115973df0db0SJames Feist             [self](const boost::system::error_code ec,
116073df0db0SJames Feist                    dbus::utility::ManagedObjectType& managedObj) {
116173df0db0SJames Feist                 if (ec)
116273df0db0SJames Feist                 {
116373df0db0SJames Feist                     BMCWEB_LOG_ERROR << "Error communicating to Entity Manager";
116473df0db0SJames Feist                     messages::internalError(self->asyncResp->res);
116573df0db0SJames Feist                     return;
116673df0db0SJames Feist                 }
116773df0db0SJames Feist                 self->managedObj = std::move(managedObj);
116873df0db0SJames Feist             },
116973df0db0SJames Feist             "xyz.openbmc_project.EntityManager", "/", objectManagerIface,
117073df0db0SJames Feist             "GetManagedObjects");
117173df0db0SJames Feist 
117273df0db0SJames Feist         // at the same time get the profile information
117373df0db0SJames Feist         crow::connections::systemBus->async_method_call(
117473df0db0SJames Feist             [self](const boost::system::error_code ec,
117573df0db0SJames Feist                    const crow::openbmc_mapper::GetSubTreeType& subtree) {
117673df0db0SJames Feist                 if (ec || subtree.empty())
117773df0db0SJames Feist                 {
117873df0db0SJames Feist                     return;
117973df0db0SJames Feist                 }
118073df0db0SJames Feist                 if (subtree[0].second.empty())
118173df0db0SJames Feist                 {
118273df0db0SJames Feist                     // invalid mapper response, should never happen
118373df0db0SJames Feist                     BMCWEB_LOG_ERROR << "SetPIDValues: Mapper Error";
118473df0db0SJames Feist                     messages::internalError(self->asyncResp->res);
118573df0db0SJames Feist                     return;
118673df0db0SJames Feist                 }
118773df0db0SJames Feist 
118873df0db0SJames Feist                 const std::string& path = subtree[0].first;
118973df0db0SJames Feist                 const std::string& owner = subtree[0].second[0].first;
119073df0db0SJames Feist                 crow::connections::systemBus->async_method_call(
119173df0db0SJames Feist                     [self, path, owner](
119273df0db0SJames Feist                         const boost::system::error_code ec,
119373df0db0SJames Feist                         const boost::container::flat_map<
119473df0db0SJames Feist                             std::string, std::variant<std::vector<std::string>,
119573df0db0SJames Feist                                                       std::string>>& resp) {
119673df0db0SJames Feist                         if (ec)
119773df0db0SJames Feist                         {
119873df0db0SJames Feist                             BMCWEB_LOG_ERROR << "SetPIDValues: Can't get "
119973df0db0SJames Feist                                                 "thermalModeIface "
120073df0db0SJames Feist                                              << path;
120173df0db0SJames Feist                             messages::internalError(self->asyncResp->res);
120273df0db0SJames Feist                             return;
120373df0db0SJames Feist                         }
120473df0db0SJames Feist                         const std::string* current;
120573df0db0SJames Feist                         const std::vector<std::string>* supported;
120673df0db0SJames Feist                         for (auto& [key, value] : resp)
120773df0db0SJames Feist                         {
120873df0db0SJames Feist                             if (key == "Current")
120973df0db0SJames Feist                             {
121073df0db0SJames Feist                                 current = std::get_if<std::string>(&value);
121173df0db0SJames Feist                                 if (current == nullptr)
121273df0db0SJames Feist                                 {
121373df0db0SJames Feist                                     BMCWEB_LOG_ERROR
121473df0db0SJames Feist                                         << "SetPIDValues: thermal mode "
121573df0db0SJames Feist                                            "iface invalid "
121673df0db0SJames Feist                                         << path;
121773df0db0SJames Feist                                     messages::internalError(
121873df0db0SJames Feist                                         self->asyncResp->res);
121973df0db0SJames Feist                                     return;
122073df0db0SJames Feist                                 }
122173df0db0SJames Feist                             }
122273df0db0SJames Feist                             if (key == "Supported")
122373df0db0SJames Feist                             {
122473df0db0SJames Feist                                 supported =
122573df0db0SJames Feist                                     std::get_if<std::vector<std::string>>(
122673df0db0SJames Feist                                         &value);
122773df0db0SJames Feist                                 if (supported == nullptr)
122873df0db0SJames Feist                                 {
122973df0db0SJames Feist                                     BMCWEB_LOG_ERROR
123073df0db0SJames Feist                                         << "SetPIDValues: thermal mode "
123173df0db0SJames Feist                                            "iface invalid"
123273df0db0SJames Feist                                         << path;
123373df0db0SJames Feist                                     messages::internalError(
123473df0db0SJames Feist                                         self->asyncResp->res);
123573df0db0SJames Feist                                     return;
123673df0db0SJames Feist                                 }
123773df0db0SJames Feist                             }
123873df0db0SJames Feist                         }
123973df0db0SJames Feist                         if (current == nullptr || supported == nullptr)
124073df0db0SJames Feist                         {
124173df0db0SJames Feist                             BMCWEB_LOG_ERROR << "SetPIDValues: thermal mode "
124273df0db0SJames Feist                                                 "iface invalid "
124373df0db0SJames Feist                                              << path;
124473df0db0SJames Feist                             messages::internalError(self->asyncResp->res);
124573df0db0SJames Feist                             return;
124673df0db0SJames Feist                         }
124773df0db0SJames Feist                         self->currentProfile = *current;
124873df0db0SJames Feist                         self->supportedProfiles = *supported;
124973df0db0SJames Feist                         self->profileConnection = owner;
125073df0db0SJames Feist                         self->profilePath = path;
125173df0db0SJames Feist                     },
125273df0db0SJames Feist                     owner, path, "org.freedesktop.DBus.Properties", "GetAll",
125373df0db0SJames Feist                     thermalModeIface);
12545b4aa86bSJames Feist             },
12555b4aa86bSJames Feist             "xyz.openbmc_project.ObjectMapper",
12565b4aa86bSJames Feist             "/xyz/openbmc_project/object_mapper",
12575b4aa86bSJames Feist             "xyz.openbmc_project.ObjectMapper", "GetSubTree", "/", 0,
125873df0db0SJames Feist             std::array<const char*, 1>{thermalModeIface});
125973df0db0SJames Feist     }
126073df0db0SJames Feist     ~SetPIDValues()
126173df0db0SJames Feist     {
126273df0db0SJames Feist         if (asyncResp->res.result() != boost::beast::http::status::ok)
126373df0db0SJames Feist         {
126473df0db0SJames Feist             return;
12655b4aa86bSJames Feist         }
12665b4aa86bSJames Feist 
126773df0db0SJames Feist         std::shared_ptr<AsyncResp> response = asyncResp;
126873df0db0SJames Feist 
126973df0db0SJames Feist         if (profile)
127073df0db0SJames Feist         {
127173df0db0SJames Feist             if (std::find(supportedProfiles.begin(), supportedProfiles.end(),
127273df0db0SJames Feist                           *profile) == supportedProfiles.end())
127373df0db0SJames Feist             {
127473df0db0SJames Feist                 messages::actionParameterUnknown(response->res, "Profile",
127573df0db0SJames Feist                                                  *profile);
127673df0db0SJames Feist                 return;
127773df0db0SJames Feist             }
127873df0db0SJames Feist             currentProfile = *profile;
127973df0db0SJames Feist             crow::connections::systemBus->async_method_call(
128073df0db0SJames Feist                 [response](const boost::system::error_code ec) {
128173df0db0SJames Feist                     if (ec)
128273df0db0SJames Feist                     {
128373df0db0SJames Feist                         BMCWEB_LOG_ERROR << "Error patching profile" << ec;
128473df0db0SJames Feist                         messages::internalError(response->res);
128573df0db0SJames Feist                     }
128673df0db0SJames Feist                 },
128773df0db0SJames Feist                 profileConnection, profilePath,
128873df0db0SJames Feist                 "org.freedesktop.DBus.Properties", "Set", thermalModeIface,
128973df0db0SJames Feist                 "Current", std::variant<std::string>(*profile));
129073df0db0SJames Feist         }
129173df0db0SJames Feist 
129273df0db0SJames Feist         for (auto& containerPair : configuration)
129373df0db0SJames Feist         {
129473df0db0SJames Feist             auto& container = containerPair.second;
129573df0db0SJames Feist             if (!container)
129673df0db0SJames Feist             {
129773df0db0SJames Feist                 continue;
129873df0db0SJames Feist             }
129973df0db0SJames Feist             std::string& type = containerPair.first;
130073df0db0SJames Feist 
130173df0db0SJames Feist             for (nlohmann::json::iterator it = container->begin();
130273df0db0SJames Feist                  it != container->end(); it++)
130373df0db0SJames Feist             {
130473df0db0SJames Feist                 const auto& name = it.key();
130573df0db0SJames Feist                 auto pathItr =
130673df0db0SJames Feist                     std::find_if(managedObj.begin(), managedObj.end(),
130773df0db0SJames Feist                                  [&name](const auto& obj) {
130873df0db0SJames Feist                                      return boost::algorithm::ends_with(
130973df0db0SJames Feist                                          obj.first.str, "/" + name);
131073df0db0SJames Feist                                  });
131173df0db0SJames Feist                 boost::container::flat_map<std::string,
131273df0db0SJames Feist                                            dbus::utility::DbusVariantType>
131373df0db0SJames Feist                     output;
131473df0db0SJames Feist 
131573df0db0SJames Feist                 output.reserve(16); // The pid interface length
131673df0db0SJames Feist 
131773df0db0SJames Feist                 // determines if we're patching entity-manager or
131873df0db0SJames Feist                 // creating a new object
131973df0db0SJames Feist                 bool createNewObject = (pathItr == managedObj.end());
132073df0db0SJames Feist                 std::string iface;
132173df0db0SJames Feist                 if (type == "PidControllers" || type == "FanControllers")
132273df0db0SJames Feist                 {
132373df0db0SJames Feist                     iface = pidConfigurationIface;
132473df0db0SJames Feist                     if (!createNewObject &&
132573df0db0SJames Feist                         pathItr->second.find(pidConfigurationIface) ==
132673df0db0SJames Feist                             pathItr->second.end())
132773df0db0SJames Feist                     {
132873df0db0SJames Feist                         createNewObject = true;
132973df0db0SJames Feist                     }
133073df0db0SJames Feist                 }
133173df0db0SJames Feist                 else if (type == "FanZones")
133273df0db0SJames Feist                 {
133373df0db0SJames Feist                     iface = pidZoneConfigurationIface;
133473df0db0SJames Feist                     if (!createNewObject &&
133573df0db0SJames Feist                         pathItr->second.find(pidZoneConfigurationIface) ==
133673df0db0SJames Feist                             pathItr->second.end())
133773df0db0SJames Feist                     {
133873df0db0SJames Feist 
133973df0db0SJames Feist                         createNewObject = true;
134073df0db0SJames Feist                     }
134173df0db0SJames Feist                 }
134273df0db0SJames Feist                 else if (type == "StepwiseControllers")
134373df0db0SJames Feist                 {
134473df0db0SJames Feist                     iface = stepwiseConfigurationIface;
134573df0db0SJames Feist                     if (!createNewObject &&
134673df0db0SJames Feist                         pathItr->second.find(stepwiseConfigurationIface) ==
134773df0db0SJames Feist                             pathItr->second.end())
134873df0db0SJames Feist                     {
134973df0db0SJames Feist                         createNewObject = true;
135073df0db0SJames Feist                     }
135173df0db0SJames Feist                 }
135273df0db0SJames Feist                 BMCWEB_LOG_DEBUG << "Create new = " << createNewObject << "\n";
135373df0db0SJames Feist                 output["Name"] = boost::replace_all_copy(name, "_", " ");
135473df0db0SJames Feist 
135573df0db0SJames Feist                 std::string chassis;
135673df0db0SJames Feist                 CreatePIDRet ret = createPidInterface(
135773df0db0SJames Feist                     response, type, it, pathItr->first.str, managedObj,
135873df0db0SJames Feist                     createNewObject, output, chassis, currentProfile);
135973df0db0SJames Feist                 if (ret == CreatePIDRet::fail)
136073df0db0SJames Feist                 {
136173df0db0SJames Feist                     return;
136273df0db0SJames Feist                 }
136373df0db0SJames Feist                 else if (ret == CreatePIDRet::del)
136473df0db0SJames Feist                 {
136573df0db0SJames Feist                     continue;
136673df0db0SJames Feist                 }
136773df0db0SJames Feist 
136873df0db0SJames Feist                 if (!createNewObject)
136973df0db0SJames Feist                 {
137073df0db0SJames Feist                     for (const auto& property : output)
137173df0db0SJames Feist                     {
137273df0db0SJames Feist                         crow::connections::systemBus->async_method_call(
137373df0db0SJames Feist                             [response,
137473df0db0SJames Feist                              propertyName{std::string(property.first)}](
137573df0db0SJames Feist                                 const boost::system::error_code ec) {
137673df0db0SJames Feist                                 if (ec)
137773df0db0SJames Feist                                 {
137873df0db0SJames Feist                                     BMCWEB_LOG_ERROR << "Error patching "
137973df0db0SJames Feist                                                      << propertyName << ": "
138073df0db0SJames Feist                                                      << ec;
138173df0db0SJames Feist                                     messages::internalError(response->res);
138273df0db0SJames Feist                                     return;
138373df0db0SJames Feist                                 }
138473df0db0SJames Feist                                 messages::success(response->res);
138573df0db0SJames Feist                             },
138673df0db0SJames Feist                             "xyz.openbmc_project.EntityManager",
138773df0db0SJames Feist                             pathItr->first.str,
138873df0db0SJames Feist                             "org.freedesktop.DBus.Properties", "Set", iface,
138973df0db0SJames Feist                             property.first, property.second);
139073df0db0SJames Feist                     }
139173df0db0SJames Feist                 }
139273df0db0SJames Feist                 else
139373df0db0SJames Feist                 {
139473df0db0SJames Feist                     if (chassis.empty())
139573df0db0SJames Feist                     {
139673df0db0SJames Feist                         BMCWEB_LOG_ERROR << "Failed to get chassis from config";
139773df0db0SJames Feist                         messages::invalidObject(response->res, name);
139873df0db0SJames Feist                         return;
139973df0db0SJames Feist                     }
140073df0db0SJames Feist 
140173df0db0SJames Feist                     bool foundChassis = false;
140273df0db0SJames Feist                     for (const auto& obj : managedObj)
140373df0db0SJames Feist                     {
140473df0db0SJames Feist                         if (boost::algorithm::ends_with(obj.first.str, chassis))
140573df0db0SJames Feist                         {
140673df0db0SJames Feist                             chassis = obj.first.str;
140773df0db0SJames Feist                             foundChassis = true;
140873df0db0SJames Feist                             break;
140973df0db0SJames Feist                         }
141073df0db0SJames Feist                     }
141173df0db0SJames Feist                     if (!foundChassis)
141273df0db0SJames Feist                     {
141373df0db0SJames Feist                         BMCWEB_LOG_ERROR << "Failed to find chassis on dbus";
141473df0db0SJames Feist                         messages::resourceMissingAtURI(
141573df0db0SJames Feist                             response->res, "/redfish/v1/Chassis/" + chassis);
141673df0db0SJames Feist                         return;
141773df0db0SJames Feist                     }
141873df0db0SJames Feist 
141973df0db0SJames Feist                     crow::connections::systemBus->async_method_call(
142073df0db0SJames Feist                         [response](const boost::system::error_code ec) {
142173df0db0SJames Feist                             if (ec)
142273df0db0SJames Feist                             {
142373df0db0SJames Feist                                 BMCWEB_LOG_ERROR << "Error Adding Pid Object "
142473df0db0SJames Feist                                                  << ec;
142573df0db0SJames Feist                                 messages::internalError(response->res);
142673df0db0SJames Feist                                 return;
142773df0db0SJames Feist                             }
142873df0db0SJames Feist                             messages::success(response->res);
142973df0db0SJames Feist                         },
143073df0db0SJames Feist                         "xyz.openbmc_project.EntityManager", chassis,
143173df0db0SJames Feist                         "xyz.openbmc_project.AddObject", "AddObject", output);
143273df0db0SJames Feist                 }
143373df0db0SJames Feist             }
143473df0db0SJames Feist         }
143573df0db0SJames Feist     }
143673df0db0SJames Feist     std::shared_ptr<AsyncResp> asyncResp;
143773df0db0SJames Feist     std::vector<std::pair<std::string, std::optional<nlohmann::json>>>
143873df0db0SJames Feist         configuration;
143973df0db0SJames Feist     std::optional<std::string> profile;
144073df0db0SJames Feist     dbus::utility::ManagedObjectType managedObj;
144173df0db0SJames Feist     std::vector<std::string> supportedProfiles;
144273df0db0SJames Feist     std::string currentProfile;
144373df0db0SJames Feist     std::string profileConnection;
144473df0db0SJames Feist     std::string profilePath;
144573df0db0SJames Feist };
144673df0db0SJames Feist 
144773df0db0SJames Feist class Manager : public Node
144873df0db0SJames Feist {
144973df0db0SJames Feist   public:
145073df0db0SJames Feist     Manager(CrowApp& app) : Node(app, "/redfish/v1/Managers/bmc/")
145173df0db0SJames Feist     {
145273df0db0SJames Feist         uuid = app.template getMiddleware<crow::persistent_data::Middleware>()
145373df0db0SJames Feist                    .systemUuid;
145473df0db0SJames Feist         entityPrivileges = {
145573df0db0SJames Feist             {boost::beast::http::verb::get, {{"Login"}}},
145673df0db0SJames Feist             {boost::beast::http::verb::head, {{"Login"}}},
145773df0db0SJames Feist             {boost::beast::http::verb::patch, {{"ConfigureManager"}}},
145873df0db0SJames Feist             {boost::beast::http::verb::put, {{"ConfigureManager"}}},
145973df0db0SJames Feist             {boost::beast::http::verb::delete_, {{"ConfigureManager"}}},
146073df0db0SJames Feist             {boost::beast::http::verb::post, {{"ConfigureManager"}}}};
146173df0db0SJames Feist     }
146273df0db0SJames Feist 
146373df0db0SJames Feist   private:
146455c7b7a2SEd Tanous     void doGet(crow::Response& res, const crow::Request& req,
14651abe55efSEd Tanous                const std::vector<std::string>& params) override
14661abe55efSEd Tanous     {
14670f74e643SEd Tanous         res.jsonValue["@odata.id"] = "/redfish/v1/Managers/bmc";
14680f74e643SEd Tanous         res.jsonValue["@odata.type"] = "#Manager.v1_3_0.Manager";
14690f74e643SEd Tanous         res.jsonValue["@odata.context"] =
14700f74e643SEd Tanous             "/redfish/v1/$metadata#Manager.Manager";
14710f74e643SEd Tanous         res.jsonValue["Id"] = "bmc";
14720f74e643SEd Tanous         res.jsonValue["Name"] = "OpenBmc Manager";
14730f74e643SEd Tanous         res.jsonValue["Description"] = "Baseboard Management Controller";
14740f74e643SEd Tanous         res.jsonValue["PowerState"] = "On";
1475029573d4SEd Tanous         res.jsonValue["Status"] = {{"State", "Enabled"}, {"Health", "OK"}};
14760f74e643SEd Tanous         res.jsonValue["ManagerType"] = "BMC";
14773602e232SEd Tanous         res.jsonValue["UUID"] = systemd_utils::getUuid();
14783602e232SEd Tanous         res.jsonValue["ServiceEntryPointUUID"] = uuid;
14790f74e643SEd Tanous         res.jsonValue["Model"] = "OpenBmc"; // TODO(ed), get model
14800f74e643SEd Tanous 
14810f74e643SEd Tanous         res.jsonValue["LogServices"] = {
14820f74e643SEd Tanous             {"@odata.id", "/redfish/v1/Managers/bmc/LogServices"}};
14830f74e643SEd Tanous 
14840f74e643SEd Tanous         res.jsonValue["NetworkProtocol"] = {
14850f74e643SEd Tanous             {"@odata.id", "/redfish/v1/Managers/bmc/NetworkProtocol"}};
14860f74e643SEd Tanous 
14870f74e643SEd Tanous         res.jsonValue["EthernetInterfaces"] = {
14880f74e643SEd Tanous             {"@odata.id", "/redfish/v1/Managers/bmc/EthernetInterfaces"}};
14890f74e643SEd Tanous         // default oem data
14900f74e643SEd Tanous         nlohmann::json& oem = res.jsonValue["Oem"];
14910f74e643SEd Tanous         nlohmann::json& oemOpenbmc = oem["OpenBmc"];
14920f74e643SEd Tanous         oem["@odata.type"] = "#OemManager.Oem";
14930f74e643SEd Tanous         oem["@odata.id"] = "/redfish/v1/Managers/bmc#/Oem";
14940f74e643SEd Tanous         oem["@odata.context"] = "/redfish/v1/$metadata#OemManager.Oem";
14950f74e643SEd Tanous         oemOpenbmc["@odata.type"] = "#OemManager.OpenBmc";
14960f74e643SEd Tanous         oemOpenbmc["@odata.id"] = "/redfish/v1/Managers/bmc#/Oem/OpenBmc";
14970f74e643SEd Tanous         oemOpenbmc["@odata.context"] =
14980f74e643SEd Tanous             "/redfish/v1/$metadata#OemManager.OpenBmc";
14990f74e643SEd Tanous 
1500ed5befbdSJennifer Lee         // Update Actions object.
15010f74e643SEd Tanous         nlohmann::json& manager_reset =
15020f74e643SEd Tanous             res.jsonValue["Actions"]["#Manager.Reset"];
1503ed5befbdSJennifer Lee         manager_reset["target"] =
1504ed5befbdSJennifer Lee             "/redfish/v1/Managers/bmc/Actions/Manager.Reset";
1505ed5befbdSJennifer Lee         manager_reset["ResetType@Redfish.AllowableValues"] = {
1506ed5befbdSJennifer Lee             "GracefulRestart"};
1507ca537928SJennifer Lee 
1508cb92c03bSAndrew Geissler         res.jsonValue["DateTime"] = crow::utility::dateTimeNow();
1509474bfad5SSantosh Puranik 
1510474bfad5SSantosh Puranik         // Fill in GraphicalConsole and SerialConsole info
1511474bfad5SSantosh Puranik         res.jsonValue["SerialConsole"]["ServiceEnabled"] = true;
1512474bfad5SSantosh Puranik         res.jsonValue["SerialConsole"]["ConnectTypesSupported"] = {"IPMI",
1513474bfad5SSantosh Puranik                                                                    "SSH"};
1514ef47bb18SSantosh Puranik #ifdef BMCWEB_ENABLE_KVM
1515ef47bb18SSantosh Puranik         res.jsonValue["GraphicalConsole"]["ServiceEnabled"] = true;
1516ef47bb18SSantosh Puranik         res.jsonValue["GraphicalConsole"]["ConnectTypesSupported"] = {"KVMIP"};
1517ef47bb18SSantosh Puranik #endif // BMCWEB_ENABLE_KVM
1518474bfad5SSantosh Puranik 
1519603a6640SGunnar Mills         res.jsonValue["Links"]["ManagerForServers@odata.count"] = 1;
1520603a6640SGunnar Mills         res.jsonValue["Links"]["ManagerForServers"] = {
1521603a6640SGunnar Mills             {{"@odata.id", "/redfish/v1/Systems/system"}}};
152226f03899SShawn McCarney 
1523ed5befbdSJennifer Lee         std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
15245b4aa86bSJames Feist 
1525ca537928SJennifer Lee         crow::connections::systemBus->async_method_call(
1526ca537928SJennifer Lee             [asyncResp](const boost::system::error_code ec,
15275b4aa86bSJames Feist                         const dbus::utility::ManagedObjectType& resp) {
1528ca537928SJennifer Lee                 if (ec)
1529ca537928SJennifer Lee                 {
1530ca537928SJennifer Lee                     BMCWEB_LOG_ERROR << "Error while getting Software Version";
1531f12894f8SJason M. Bills                     messages::internalError(asyncResp->res);
1532ca537928SJennifer Lee                     return;
1533ca537928SJennifer Lee                 }
1534ca537928SJennifer Lee 
1535ca537928SJennifer Lee                 for (auto& objpath : resp)
1536ca537928SJennifer Lee                 {
1537ca537928SJennifer Lee                     for (auto& interface : objpath.second)
1538ca537928SJennifer Lee                     {
15395f2caaefSJames Feist                         // If interface is
15405f2caaefSJames Feist                         // xyz.openbmc_project.Software.Version, this is
15415f2caaefSJames Feist                         // what we're looking for.
1542ca537928SJennifer Lee                         if (interface.first ==
1543ca537928SJennifer Lee                             "xyz.openbmc_project.Software.Version")
1544ca537928SJennifer Lee                         {
1545ca537928SJennifer Lee                             // Cut out everyting until last "/", ...
1546ca537928SJennifer Lee                             for (auto& property : interface.second)
1547ca537928SJennifer Lee                             {
1548ca537928SJennifer Lee                                 if (property.first == "Version")
1549ca537928SJennifer Lee                                 {
1550ca537928SJennifer Lee                                     const std::string* value =
1551abf2add6SEd Tanous                                         std::get_if<std::string>(
1552abf2add6SEd Tanous                                             &property.second);
1553ca537928SJennifer Lee                                     if (value == nullptr)
1554ca537928SJennifer Lee                                     {
1555ca537928SJennifer Lee                                         continue;
1556ca537928SJennifer Lee                                     }
1557ca537928SJennifer Lee                                     asyncResp->res
1558ca537928SJennifer Lee                                         .jsonValue["FirmwareVersion"] = *value;
1559ca537928SJennifer Lee                                 }
1560ca537928SJennifer Lee                             }
1561ca537928SJennifer Lee                         }
1562ca537928SJennifer Lee                     }
1563ca537928SJennifer Lee                 }
1564ca537928SJennifer Lee             },
1565ca537928SJennifer Lee             "xyz.openbmc_project.Software.BMC.Updater",
1566ca537928SJennifer Lee             "/xyz/openbmc_project/software",
1567ca537928SJennifer Lee             "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
1568*0f6b00bdSJames Feist 
156973df0db0SJames Feist         auto pids = std::make_shared<GetPIDValues>(asyncResp);
157073df0db0SJames Feist         pids->run();
1571c5d03ff4SJennifer Lee 
1572c5d03ff4SJennifer Lee         getMainChassisId(asyncResp, [](const std::string& chassisId,
1573c5d03ff4SJennifer Lee                                        const std::shared_ptr<AsyncResp> aRsp) {
1574c5d03ff4SJennifer Lee             aRsp->res.jsonValue["Links"]["ManagerForChassis@odata.count"] = 1;
1575c5d03ff4SJennifer Lee             aRsp->res.jsonValue["Links"]["ManagerForChassis"] = {
1576c5d03ff4SJennifer Lee                 {{"@odata.id", "/redfish/v1/Chassis/" + chassisId}}};
1577c5d03ff4SJennifer Lee         });
1578*0f6b00bdSJames Feist 
1579*0f6b00bdSJames Feist         static bool started = false;
1580*0f6b00bdSJames Feist 
1581*0f6b00bdSJames Feist         if (!started)
1582*0f6b00bdSJames Feist         {
1583*0f6b00bdSJames Feist             crow::connections::systemBus->async_method_call(
1584*0f6b00bdSJames Feist                 [asyncResp](const boost::system::error_code ec,
1585*0f6b00bdSJames Feist                             const std::variant<double>& resp) {
1586*0f6b00bdSJames Feist                     if (ec)
1587*0f6b00bdSJames Feist                     {
1588*0f6b00bdSJames Feist                         BMCWEB_LOG_ERROR << "Error while getting progress";
1589*0f6b00bdSJames Feist                         messages::internalError(asyncResp->res);
1590*0f6b00bdSJames Feist                         return;
1591*0f6b00bdSJames Feist                     }
1592*0f6b00bdSJames Feist                     const double* val = std::get_if<double>(&resp);
1593*0f6b00bdSJames Feist                     if (val == nullptr)
1594*0f6b00bdSJames Feist                     {
1595*0f6b00bdSJames Feist                         BMCWEB_LOG_ERROR
1596*0f6b00bdSJames Feist                             << "Invalid response while getting progress";
1597*0f6b00bdSJames Feist                         messages::internalError(asyncResp->res);
1598*0f6b00bdSJames Feist                         return;
1599*0f6b00bdSJames Feist                     }
1600*0f6b00bdSJames Feist                     if (*val < 1.0)
1601*0f6b00bdSJames Feist                     {
1602*0f6b00bdSJames Feist                         asyncResp->res.jsonValue["Status"]["State"] =
1603*0f6b00bdSJames Feist                             "Starting";
1604*0f6b00bdSJames Feist                         started = true;
1605*0f6b00bdSJames Feist                     }
1606*0f6b00bdSJames Feist                 },
1607*0f6b00bdSJames Feist                 "org.freedesktop.systemd1", "/org/freedesktop/systemd1",
1608*0f6b00bdSJames Feist                 "org.freedesktop.DBus.Properties", "Get",
1609*0f6b00bdSJames Feist                 "org.freedesktop.systemd1.Manager", "Progress");
1610*0f6b00bdSJames Feist         }
161183ff9ab6SJames Feist     }
16125b4aa86bSJames Feist 
16135b4aa86bSJames Feist     void doPatch(crow::Response& res, const crow::Request& req,
16145b4aa86bSJames Feist                  const std::vector<std::string>& params) override
16155b4aa86bSJames Feist     {
16160627a2c7SEd Tanous         std::optional<nlohmann::json> oem;
1617af5d6058SSantosh Puranik         std::optional<std::string> datetime;
16180627a2c7SEd Tanous 
1619af5d6058SSantosh Puranik         if (!json_util::readJson(req, res, "Oem", oem, "DateTime", datetime))
162083ff9ab6SJames Feist         {
162183ff9ab6SJames Feist             return;
162283ff9ab6SJames Feist         }
16230627a2c7SEd Tanous 
162483ff9ab6SJames Feist         std::shared_ptr<AsyncResp> response = std::make_shared<AsyncResp>(res);
16250627a2c7SEd Tanous 
16260627a2c7SEd Tanous         if (oem)
162783ff9ab6SJames Feist         {
16285f2caaefSJames Feist             std::optional<nlohmann::json> openbmc;
162943b761d0SEd Tanous             if (!redfish::json_util::readJson(*oem, res, "OpenBmc", openbmc))
163083ff9ab6SJames Feist             {
163143b761d0SEd Tanous                 BMCWEB_LOG_ERROR << "Line:" << __LINE__ << ", Illegal Property "
163243b761d0SEd Tanous                                  << oem->dump();
163383ff9ab6SJames Feist                 return;
163483ff9ab6SJames Feist             }
16355f2caaefSJames Feist             if (openbmc)
163683ff9ab6SJames Feist             {
16375f2caaefSJames Feist                 std::optional<nlohmann::json> fan;
163843b761d0SEd Tanous                 if (!redfish::json_util::readJson(*openbmc, res, "Fan", fan))
163983ff9ab6SJames Feist                 {
16405f2caaefSJames Feist                     BMCWEB_LOG_ERROR << "Line:" << __LINE__
16415f2caaefSJames Feist                                      << ", Illegal Property "
16425f2caaefSJames Feist                                      << openbmc->dump();
164383ff9ab6SJames Feist                     return;
164483ff9ab6SJames Feist                 }
16455f2caaefSJames Feist                 if (fan)
164683ff9ab6SJames Feist                 {
164773df0db0SJames Feist                     auto pid = std::make_shared<SetPIDValues>(response, *fan);
164873df0db0SJames Feist                     pid->run();
164983ff9ab6SJames Feist                 }
165083ff9ab6SJames Feist             }
165183ff9ab6SJames Feist         }
1652af5d6058SSantosh Puranik         if (datetime)
1653af5d6058SSantosh Puranik         {
1654af5d6058SSantosh Puranik             setDateTime(response, std::move(*datetime));
1655af5d6058SSantosh Puranik         }
1656af5d6058SSantosh Puranik     }
1657af5d6058SSantosh Puranik 
1658af5d6058SSantosh Puranik     void setDateTime(std::shared_ptr<AsyncResp> aResp,
1659af5d6058SSantosh Puranik                      std::string datetime) const
1660af5d6058SSantosh Puranik     {
1661af5d6058SSantosh Puranik         BMCWEB_LOG_DEBUG << "Set date time: " << datetime;
1662af5d6058SSantosh Puranik 
1663af5d6058SSantosh Puranik         std::stringstream stream(datetime);
1664af5d6058SSantosh Puranik         // Convert from ISO 8601 to boost local_time
1665af5d6058SSantosh Puranik         // (BMC only has time in UTC)
1666af5d6058SSantosh Puranik         boost::posix_time::ptime posixTime;
1667af5d6058SSantosh Puranik         boost::posix_time::ptime epoch(boost::gregorian::date(1970, 1, 1));
1668af5d6058SSantosh Puranik         // Facet gets deleted with the stringsteam
1669af5d6058SSantosh Puranik         auto ifc = std::make_unique<boost::local_time::local_time_input_facet>(
1670af5d6058SSantosh Puranik             "%Y-%m-%d %H:%M:%S%F %ZP");
1671af5d6058SSantosh Puranik         stream.imbue(std::locale(stream.getloc(), ifc.release()));
1672af5d6058SSantosh Puranik 
1673af5d6058SSantosh Puranik         boost::local_time::local_date_time ldt(
1674af5d6058SSantosh Puranik             boost::local_time::not_a_date_time);
1675af5d6058SSantosh Puranik 
1676af5d6058SSantosh Puranik         if (stream >> ldt)
1677af5d6058SSantosh Puranik         {
1678af5d6058SSantosh Puranik             posixTime = ldt.utc_time();
1679af5d6058SSantosh Puranik             boost::posix_time::time_duration dur = posixTime - epoch;
1680af5d6058SSantosh Puranik             uint64_t durMicroSecs =
1681af5d6058SSantosh Puranik                 static_cast<uint64_t>(dur.total_microseconds());
1682af5d6058SSantosh Puranik             crow::connections::systemBus->async_method_call(
1683af5d6058SSantosh Puranik                 [aResp{std::move(aResp)}, datetime{std::move(datetime)}](
1684af5d6058SSantosh Puranik                     const boost::system::error_code ec) {
1685af5d6058SSantosh Puranik                     if (ec)
1686af5d6058SSantosh Puranik                     {
1687af5d6058SSantosh Puranik                         BMCWEB_LOG_DEBUG << "Failed to set elapsed time. "
1688af5d6058SSantosh Puranik                                             "DBUS response error "
1689af5d6058SSantosh Puranik                                          << ec;
1690af5d6058SSantosh Puranik                         messages::internalError(aResp->res);
1691af5d6058SSantosh Puranik                         return;
1692af5d6058SSantosh Puranik                     }
1693af5d6058SSantosh Puranik                     aResp->res.jsonValue["DateTime"] = datetime;
1694af5d6058SSantosh Puranik                 },
1695af5d6058SSantosh Puranik                 "xyz.openbmc_project.Time.Manager",
1696af5d6058SSantosh Puranik                 "/xyz/openbmc_project/time/bmc",
1697af5d6058SSantosh Puranik                 "org.freedesktop.DBus.Properties", "Set",
1698af5d6058SSantosh Puranik                 "xyz.openbmc_project.Time.EpochTime", "Elapsed",
1699af5d6058SSantosh Puranik                 std::variant<uint64_t>(durMicroSecs));
1700af5d6058SSantosh Puranik         }
1701af5d6058SSantosh Puranik         else
1702af5d6058SSantosh Puranik         {
1703af5d6058SSantosh Puranik             messages::propertyValueFormatError(aResp->res, datetime,
1704af5d6058SSantosh Puranik                                                "DateTime");
1705af5d6058SSantosh Puranik             return;
1706af5d6058SSantosh Puranik         }
170783ff9ab6SJames Feist     }
17089c310685SBorawski.Lukasz 
17090f74e643SEd Tanous     std::string uuid;
17109c310685SBorawski.Lukasz };
17119c310685SBorawski.Lukasz 
17121abe55efSEd Tanous class ManagerCollection : public Node
17131abe55efSEd Tanous {
17149c310685SBorawski.Lukasz   public:
17151abe55efSEd Tanous     ManagerCollection(CrowApp& app) : Node(app, "/redfish/v1/Managers/")
17161abe55efSEd Tanous     {
1717a434f2bdSEd Tanous         entityPrivileges = {
1718a434f2bdSEd Tanous             {boost::beast::http::verb::get, {{"Login"}}},
1719e0d918bcSEd Tanous             {boost::beast::http::verb::head, {{"Login"}}},
1720e0d918bcSEd Tanous             {boost::beast::http::verb::patch, {{"ConfigureManager"}}},
1721e0d918bcSEd Tanous             {boost::beast::http::verb::put, {{"ConfigureManager"}}},
1722e0d918bcSEd Tanous             {boost::beast::http::verb::delete_, {{"ConfigureManager"}}},
1723e0d918bcSEd Tanous             {boost::beast::http::verb::post, {{"ConfigureManager"}}}};
17249c310685SBorawski.Lukasz     }
17259c310685SBorawski.Lukasz 
17269c310685SBorawski.Lukasz   private:
172755c7b7a2SEd Tanous     void doGet(crow::Response& res, const crow::Request& req,
17281abe55efSEd Tanous                const std::vector<std::string>& params) override
17291abe55efSEd Tanous     {
173083ff9ab6SJames Feist         // Collections don't include the static data added by SubRoute
173183ff9ab6SJames Feist         // because it has a duplicate entry for members
173255c7b7a2SEd Tanous         res.jsonValue["@odata.id"] = "/redfish/v1/Managers";
173355c7b7a2SEd Tanous         res.jsonValue["@odata.type"] = "#ManagerCollection.ManagerCollection";
173455c7b7a2SEd Tanous         res.jsonValue["@odata.context"] =
173555c7b7a2SEd Tanous             "/redfish/v1/$metadata#ManagerCollection.ManagerCollection";
173655c7b7a2SEd Tanous         res.jsonValue["Name"] = "Manager Collection";
173755c7b7a2SEd Tanous         res.jsonValue["Members@odata.count"] = 1;
173855c7b7a2SEd Tanous         res.jsonValue["Members"] = {
17395b4aa86bSJames Feist             {{"@odata.id", "/redfish/v1/Managers/bmc"}}};
17409c310685SBorawski.Lukasz         res.end();
17419c310685SBorawski.Lukasz     }
17429c310685SBorawski.Lukasz };
17439c310685SBorawski.Lukasz } // namespace redfish
1744