xref: /openbmc/bmcweb/features/redfish/lib/managers.hpp (revision b49ac87376278d6085c1d10815672c321a484f35)
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 
18*b49ac873SJames Feist #include "health.hpp"
199c310685SBorawski.Lukasz #include "node.hpp"
20c5d03ff4SJennifer Lee #include "redfish_util.hpp"
219c310685SBorawski.Lukasz 
225b4aa86bSJames Feist #include <boost/algorithm/string/replace.hpp>
23af5d6058SSantosh Puranik #include <boost/date_time.hpp>
245b4aa86bSJames Feist #include <dbus_utility.hpp>
2573df0db0SJames Feist #include <memory>
26af5d6058SSantosh Puranik #include <sstream>
277bffdb7eSBernard Wong #include <utils/systemd_utils.hpp>
28abf2add6SEd Tanous #include <variant>
295b4aa86bSJames Feist 
301abe55efSEd Tanous namespace redfish
311abe55efSEd Tanous {
32ed5befbdSJennifer Lee 
33ed5befbdSJennifer Lee /**
34ed5befbdSJennifer Lee  * ManagerActionsReset class supports handle POST method for Reset action.
35ed5befbdSJennifer Lee  * The class retrieves and sends data directly to dbus.
36ed5befbdSJennifer Lee  */
37ed5befbdSJennifer Lee class ManagerActionsReset : public Node
38ed5befbdSJennifer Lee {
39ed5befbdSJennifer Lee   public:
40ed5befbdSJennifer Lee     ManagerActionsReset(CrowApp& app) :
41ed5befbdSJennifer Lee         Node(app, "/redfish/v1/Managers/bmc/Actions/Manager.Reset/")
42ed5befbdSJennifer Lee     {
43ed5befbdSJennifer Lee         entityPrivileges = {
44ed5befbdSJennifer Lee             {boost::beast::http::verb::get, {{"Login"}}},
45ed5befbdSJennifer Lee             {boost::beast::http::verb::head, {{"Login"}}},
46ed5befbdSJennifer Lee             {boost::beast::http::verb::patch, {{"ConfigureManager"}}},
47ed5befbdSJennifer Lee             {boost::beast::http::verb::put, {{"ConfigureManager"}}},
48ed5befbdSJennifer Lee             {boost::beast::http::verb::delete_, {{"ConfigureManager"}}},
49ed5befbdSJennifer Lee             {boost::beast::http::verb::post, {{"ConfigureManager"}}}};
50ed5befbdSJennifer Lee     }
51ed5befbdSJennifer Lee 
52ed5befbdSJennifer Lee   private:
53ed5befbdSJennifer Lee     /**
54ed5befbdSJennifer Lee      * Function handles POST method request.
55ed5befbdSJennifer Lee      * Analyzes POST body message before sends Reset request data to dbus.
56ed5befbdSJennifer Lee      * OpenBMC allows for ResetType is GracefulRestart only.
57ed5befbdSJennifer Lee      */
58ed5befbdSJennifer Lee     void doPost(crow::Response& res, const crow::Request& req,
59ed5befbdSJennifer Lee                 const std::vector<std::string>& params) override
60ed5befbdSJennifer Lee     {
61ed5befbdSJennifer Lee         std::string resetType;
62ed5befbdSJennifer Lee 
63ed5befbdSJennifer Lee         if (!json_util::readJson(req, res, "ResetType", resetType))
64ed5befbdSJennifer Lee         {
65ed5befbdSJennifer Lee             return;
66ed5befbdSJennifer Lee         }
67ed5befbdSJennifer Lee 
68ed5befbdSJennifer Lee         if (resetType != "GracefulRestart")
69ed5befbdSJennifer Lee         {
70ed5befbdSJennifer Lee             res.result(boost::beast::http::status::bad_request);
71ed5befbdSJennifer Lee             messages::actionParameterNotSupported(res, resetType, "ResetType");
72ed5befbdSJennifer Lee             BMCWEB_LOG_ERROR << "Request incorrect action parameter: "
73ed5befbdSJennifer Lee                              << resetType;
74ed5befbdSJennifer Lee             res.end();
75ed5befbdSJennifer Lee             return;
76ed5befbdSJennifer Lee         }
77ed5befbdSJennifer Lee         doBMCGracefulRestart(res, req, params);
78ed5befbdSJennifer Lee     }
79ed5befbdSJennifer Lee 
80ed5befbdSJennifer Lee     /**
81ed5befbdSJennifer Lee      * Function transceives data with dbus directly.
82ed5befbdSJennifer Lee      * All BMC state properties will be retrieved before sending reset request.
83ed5befbdSJennifer Lee      */
84ed5befbdSJennifer Lee     void doBMCGracefulRestart(crow::Response& res, const crow::Request& req,
85ed5befbdSJennifer Lee                               const std::vector<std::string>& params)
86ed5befbdSJennifer Lee     {
87ed5befbdSJennifer Lee         const char* processName = "xyz.openbmc_project.State.BMC";
88ed5befbdSJennifer Lee         const char* objectPath = "/xyz/openbmc_project/state/bmc0";
89ed5befbdSJennifer Lee         const char* interfaceName = "xyz.openbmc_project.State.BMC";
90ed5befbdSJennifer Lee         const std::string& propertyValue =
91ed5befbdSJennifer Lee             "xyz.openbmc_project.State.BMC.Transition.Reboot";
92ed5befbdSJennifer Lee         const char* destProperty = "RequestedBMCTransition";
93ed5befbdSJennifer Lee 
94ed5befbdSJennifer Lee         // Create the D-Bus variant for D-Bus call.
95ed5befbdSJennifer Lee         VariantType dbusPropertyValue(propertyValue);
96ed5befbdSJennifer Lee 
97ed5befbdSJennifer Lee         std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
98ed5befbdSJennifer Lee 
99ed5befbdSJennifer Lee         crow::connections::systemBus->async_method_call(
100ed5befbdSJennifer Lee             [asyncResp](const boost::system::error_code ec) {
101ed5befbdSJennifer Lee                 // Use "Set" method to set the property value.
102ed5befbdSJennifer Lee                 if (ec)
103ed5befbdSJennifer Lee                 {
104ed5befbdSJennifer Lee                     BMCWEB_LOG_ERROR << "[Set] Bad D-Bus request error: " << ec;
105ed5befbdSJennifer Lee                     messages::internalError(asyncResp->res);
106ed5befbdSJennifer Lee                     return;
107ed5befbdSJennifer Lee                 }
108ed5befbdSJennifer Lee 
109ed5befbdSJennifer Lee                 messages::success(asyncResp->res);
110ed5befbdSJennifer Lee             },
111ed5befbdSJennifer Lee             processName, objectPath, "org.freedesktop.DBus.Properties", "Set",
112ed5befbdSJennifer Lee             interfaceName, destProperty, dbusPropertyValue);
113ed5befbdSJennifer Lee     }
114ed5befbdSJennifer Lee };
115ed5befbdSJennifer Lee 
1165b4aa86bSJames Feist static constexpr const char* objectManagerIface =
1175b4aa86bSJames Feist     "org.freedesktop.DBus.ObjectManager";
1185b4aa86bSJames Feist static constexpr const char* pidConfigurationIface =
1195b4aa86bSJames Feist     "xyz.openbmc_project.Configuration.Pid";
1205b4aa86bSJames Feist static constexpr const char* pidZoneConfigurationIface =
1215b4aa86bSJames Feist     "xyz.openbmc_project.Configuration.Pid.Zone";
122b7a08d04SJames Feist static constexpr const char* stepwiseConfigurationIface =
123b7a08d04SJames Feist     "xyz.openbmc_project.Configuration.Stepwise";
12473df0db0SJames Feist static constexpr const char* thermalModeIface =
12573df0db0SJames Feist     "xyz.openbmc_project.Control.ThermalMode";
1269c310685SBorawski.Lukasz 
1275b4aa86bSJames Feist static void asyncPopulatePid(const std::string& connection,
1285b4aa86bSJames Feist                              const std::string& path,
12973df0db0SJames Feist                              const std::string& currentProfile,
13073df0db0SJames Feist                              const std::vector<std::string>& supportedProfiles,
1315b4aa86bSJames Feist                              std::shared_ptr<AsyncResp> asyncResp)
1325b4aa86bSJames Feist {
1335b4aa86bSJames Feist 
1345b4aa86bSJames Feist     crow::connections::systemBus->async_method_call(
13573df0db0SJames Feist         [asyncResp, currentProfile, supportedProfiles](
13673df0db0SJames Feist             const boost::system::error_code ec,
1375b4aa86bSJames Feist             const dbus::utility::ManagedObjectType& managedObj) {
1385b4aa86bSJames Feist             if (ec)
1395b4aa86bSJames Feist             {
1405b4aa86bSJames Feist                 BMCWEB_LOG_ERROR << ec;
1415b4aa86bSJames Feist                 asyncResp->res.jsonValue.clear();
142f12894f8SJason M. Bills                 messages::internalError(asyncResp->res);
1435b4aa86bSJames Feist                 return;
1445b4aa86bSJames Feist             }
1455b4aa86bSJames Feist             nlohmann::json& configRoot =
1465b4aa86bSJames Feist                 asyncResp->res.jsonValue["Oem"]["OpenBmc"]["Fan"];
1475b4aa86bSJames Feist             nlohmann::json& fans = configRoot["FanControllers"];
1485b4aa86bSJames Feist             fans["@odata.type"] = "#OemManager.FanControllers";
1495b4aa86bSJames Feist             fans["@odata.context"] =
1505b4aa86bSJames Feist                 "/redfish/v1/$metadata#OemManager.FanControllers";
1515b4aa86bSJames Feist             fans["@odata.id"] = "/redfish/v1/Managers/bmc#/Oem/OpenBmc/"
1525b4aa86bSJames Feist                                 "Fan/FanControllers";
1535b4aa86bSJames Feist 
1545b4aa86bSJames Feist             nlohmann::json& pids = configRoot["PidControllers"];
1555b4aa86bSJames Feist             pids["@odata.type"] = "#OemManager.PidControllers";
1565b4aa86bSJames Feist             pids["@odata.context"] =
1575b4aa86bSJames Feist                 "/redfish/v1/$metadata#OemManager.PidControllers";
1585b4aa86bSJames Feist             pids["@odata.id"] =
1595b4aa86bSJames Feist                 "/redfish/v1/Managers/bmc#/Oem/OpenBmc/Fan/PidControllers";
1605b4aa86bSJames Feist 
161b7a08d04SJames Feist             nlohmann::json& stepwise = configRoot["StepwiseControllers"];
162b7a08d04SJames Feist             stepwise["@odata.type"] = "#OemManager.StepwiseControllers";
163b7a08d04SJames Feist             stepwise["@odata.context"] =
164b7a08d04SJames Feist                 "/redfish/v1/$metadata#OemManager.StepwiseControllers";
165b7a08d04SJames Feist             stepwise["@odata.id"] =
166b7a08d04SJames Feist                 "/redfish/v1/Managers/bmc#/Oem/OpenBmc/Fan/StepwiseControllers";
167b7a08d04SJames Feist 
1685b4aa86bSJames Feist             nlohmann::json& zones = configRoot["FanZones"];
1695b4aa86bSJames Feist             zones["@odata.id"] =
1705b4aa86bSJames Feist                 "/redfish/v1/Managers/bmc#/Oem/OpenBmc/Fan/FanZones";
1715b4aa86bSJames Feist             zones["@odata.type"] = "#OemManager.FanZones";
1725b4aa86bSJames Feist             zones["@odata.context"] =
1735b4aa86bSJames Feist                 "/redfish/v1/$metadata#OemManager.FanZones";
1745b4aa86bSJames Feist             configRoot["@odata.id"] =
1755b4aa86bSJames Feist                 "/redfish/v1/Managers/bmc#/Oem/OpenBmc/Fan";
1765b4aa86bSJames Feist             configRoot["@odata.type"] = "#OemManager.Fan";
1775b4aa86bSJames Feist             configRoot["@odata.context"] =
1785b4aa86bSJames Feist                 "/redfish/v1/$metadata#OemManager.Fan";
17973df0db0SJames Feist             configRoot["Profile@Redfish.AllowableValues"] = supportedProfiles;
18073df0db0SJames Feist 
18173df0db0SJames Feist             if (!currentProfile.empty())
18273df0db0SJames Feist             {
18373df0db0SJames Feist                 configRoot["Profile"] = currentProfile;
18473df0db0SJames Feist             }
18573df0db0SJames Feist             BMCWEB_LOG_ERROR << "profile = " << currentProfile << " !";
1865b4aa86bSJames Feist 
1875b4aa86bSJames Feist             for (const auto& pathPair : managedObj)
1885b4aa86bSJames Feist             {
1895b4aa86bSJames Feist                 for (const auto& intfPair : pathPair.second)
1905b4aa86bSJames Feist                 {
1915b4aa86bSJames Feist                     if (intfPair.first != pidConfigurationIface &&
192b7a08d04SJames Feist                         intfPair.first != pidZoneConfigurationIface &&
193b7a08d04SJames Feist                         intfPair.first != stepwiseConfigurationIface)
1945b4aa86bSJames Feist                     {
1955b4aa86bSJames Feist                         continue;
1965b4aa86bSJames Feist                     }
1975b4aa86bSJames Feist                     auto findName = intfPair.second.find("Name");
1985b4aa86bSJames Feist                     if (findName == intfPair.second.end())
1995b4aa86bSJames Feist                     {
2005b4aa86bSJames Feist                         BMCWEB_LOG_ERROR << "Pid Field missing Name";
201a08b46ccSJason M. Bills                         messages::internalError(asyncResp->res);
2025b4aa86bSJames Feist                         return;
2035b4aa86bSJames Feist                     }
20473df0db0SJames Feist 
2055b4aa86bSJames Feist                     const std::string* namePtr =
206abf2add6SEd Tanous                         std::get_if<std::string>(&findName->second);
2075b4aa86bSJames Feist                     if (namePtr == nullptr)
2085b4aa86bSJames Feist                     {
2095b4aa86bSJames Feist                         BMCWEB_LOG_ERROR << "Pid Name Field illegal";
210b7a08d04SJames Feist                         messages::internalError(asyncResp->res);
2115b4aa86bSJames Feist                         return;
2125b4aa86bSJames Feist                     }
2135b4aa86bSJames Feist                     std::string name = *namePtr;
2145b4aa86bSJames Feist                     dbus::utility::escapePathForDbus(name);
21573df0db0SJames Feist 
21673df0db0SJames Feist                     auto findProfiles = intfPair.second.find("Profiles");
21773df0db0SJames Feist                     if (findProfiles != intfPair.second.end())
21873df0db0SJames Feist                     {
21973df0db0SJames Feist                         const std::vector<std::string>* profiles =
22073df0db0SJames Feist                             std::get_if<std::vector<std::string>>(
22173df0db0SJames Feist                                 &findProfiles->second);
22273df0db0SJames Feist                         if (profiles == nullptr)
22373df0db0SJames Feist                         {
22473df0db0SJames Feist                             BMCWEB_LOG_ERROR << "Pid Profiles Field illegal";
22573df0db0SJames Feist                             messages::internalError(asyncResp->res);
22673df0db0SJames Feist                             return;
22773df0db0SJames Feist                         }
22873df0db0SJames Feist                         if (std::find(profiles->begin(), profiles->end(),
22973df0db0SJames Feist                                       currentProfile) == profiles->end())
23073df0db0SJames Feist                         {
23173df0db0SJames Feist                             BMCWEB_LOG_INFO
23273df0db0SJames Feist                                 << name << " not supported in current profile";
23373df0db0SJames Feist                             continue;
23473df0db0SJames Feist                         }
23573df0db0SJames Feist                     }
236b7a08d04SJames Feist                     nlohmann::json* config = nullptr;
237c33a90ecSJames Feist 
238c33a90ecSJames Feist                     const std::string* classPtr = nullptr;
239c33a90ecSJames Feist                     auto findClass = intfPair.second.find("Class");
240c33a90ecSJames Feist                     if (findClass != intfPair.second.end())
241c33a90ecSJames Feist                     {
242c33a90ecSJames Feist                         classPtr = std::get_if<std::string>(&findClass->second);
243c33a90ecSJames Feist                     }
244c33a90ecSJames Feist 
2455b4aa86bSJames Feist                     if (intfPair.first == pidZoneConfigurationIface)
2465b4aa86bSJames Feist                     {
2475b4aa86bSJames Feist                         std::string chassis;
2485b4aa86bSJames Feist                         if (!dbus::utility::getNthStringFromPath(
2495b4aa86bSJames Feist                                 pathPair.first.str, 5, chassis))
2505b4aa86bSJames Feist                         {
2515b4aa86bSJames Feist                             chassis = "#IllegalValue";
2525b4aa86bSJames Feist                         }
2535b4aa86bSJames Feist                         nlohmann::json& zone = zones[name];
2545b4aa86bSJames Feist                         zone["Chassis"] = {
2555b4aa86bSJames Feist                             {"@odata.id", "/redfish/v1/Chassis/" + chassis}};
2565b4aa86bSJames Feist                         zone["@odata.id"] = "/redfish/v1/Managers/bmc#/Oem/"
2575b4aa86bSJames Feist                                             "OpenBmc/Fan/FanZones/" +
2585b4aa86bSJames Feist                                             name;
2595b4aa86bSJames Feist                         zone["@odata.type"] = "#OemManager.FanZone";
2605b4aa86bSJames Feist                         zone["@odata.context"] =
2615b4aa86bSJames Feist                             "/redfish/v1/$metadata#OemManager.FanZone";
262b7a08d04SJames Feist                         config = &zone;
2635b4aa86bSJames Feist                     }
2645b4aa86bSJames Feist 
265b7a08d04SJames Feist                     else if (intfPair.first == stepwiseConfigurationIface)
2665b4aa86bSJames Feist                     {
267c33a90ecSJames Feist                         if (classPtr == nullptr)
268c33a90ecSJames Feist                         {
269c33a90ecSJames Feist                             BMCWEB_LOG_ERROR << "Pid Class Field illegal";
270c33a90ecSJames Feist                             messages::internalError(asyncResp->res);
271c33a90ecSJames Feist                             return;
272c33a90ecSJames Feist                         }
273c33a90ecSJames Feist 
274b7a08d04SJames Feist                         nlohmann::json& controller = stepwise[name];
275b7a08d04SJames Feist                         config = &controller;
2765b4aa86bSJames Feist 
277b7a08d04SJames Feist                         controller["@odata.id"] =
278b7a08d04SJames Feist                             "/redfish/v1/Managers/bmc#/Oem/"
279b7a08d04SJames Feist                             "OpenBmc/Fan/StepwiseControllers/" +
280b7a08d04SJames Feist                             std::string(name);
281b7a08d04SJames Feist                         controller["@odata.type"] =
282b7a08d04SJames Feist                             "#OemManager.StepwiseController";
283b7a08d04SJames Feist 
284b7a08d04SJames Feist                         controller["@odata.context"] =
285b7a08d04SJames Feist                             "/redfish/v1/"
286b7a08d04SJames Feist                             "$metadata#OemManager.StepwiseController";
287c33a90ecSJames Feist                         controller["Direction"] = *classPtr;
2885b4aa86bSJames Feist                     }
2895b4aa86bSJames Feist 
2905b4aa86bSJames Feist                     // pid and fans are off the same configuration
291b7a08d04SJames Feist                     else if (intfPair.first == pidConfigurationIface)
2925b4aa86bSJames Feist                     {
293c33a90ecSJames Feist 
2945b4aa86bSJames Feist                         if (classPtr == nullptr)
2955b4aa86bSJames Feist                         {
2965b4aa86bSJames Feist                             BMCWEB_LOG_ERROR << "Pid Class Field illegal";
297a08b46ccSJason M. Bills                             messages::internalError(asyncResp->res);
2985b4aa86bSJames Feist                             return;
2995b4aa86bSJames Feist                         }
3005b4aa86bSJames Feist                         bool isFan = *classPtr == "fan";
3015b4aa86bSJames Feist                         nlohmann::json& element =
3025b4aa86bSJames Feist                             isFan ? fans[name] : pids[name];
303b7a08d04SJames Feist                         config = &element;
3045b4aa86bSJames Feist                         if (isFan)
3055b4aa86bSJames Feist                         {
3065b4aa86bSJames Feist                             element["@odata.id"] =
3075b4aa86bSJames Feist                                 "/redfish/v1/Managers/bmc#/Oem/"
3085b4aa86bSJames Feist                                 "OpenBmc/Fan/FanControllers/" +
3095b4aa86bSJames Feist                                 std::string(name);
3105b4aa86bSJames Feist                             element["@odata.type"] =
3115b4aa86bSJames Feist                                 "#OemManager.FanController";
3125b4aa86bSJames Feist 
3135b4aa86bSJames Feist                             element["@odata.context"] =
3145b4aa86bSJames Feist                                 "/redfish/v1/"
3155b4aa86bSJames Feist                                 "$metadata#OemManager.FanController";
3165b4aa86bSJames Feist                         }
3175b4aa86bSJames Feist                         else
3185b4aa86bSJames Feist                         {
3195b4aa86bSJames Feist                             element["@odata.id"] =
3205b4aa86bSJames Feist                                 "/redfish/v1/Managers/bmc#/Oem/"
3215b4aa86bSJames Feist                                 "OpenBmc/Fan/PidControllers/" +
3225b4aa86bSJames Feist                                 std::string(name);
3235b4aa86bSJames Feist                             element["@odata.type"] =
3245b4aa86bSJames Feist                                 "#OemManager.PidController";
3255b4aa86bSJames Feist                             element["@odata.context"] =
3265b4aa86bSJames Feist                                 "/redfish/v1/$metadata"
3275b4aa86bSJames Feist                                 "#OemManager.PidController";
3285b4aa86bSJames Feist                         }
329b7a08d04SJames Feist                     }
330b7a08d04SJames Feist                     else
331b7a08d04SJames Feist                     {
332b7a08d04SJames Feist                         BMCWEB_LOG_ERROR << "Unexpected configuration";
333b7a08d04SJames Feist                         messages::internalError(asyncResp->res);
334b7a08d04SJames Feist                         return;
335b7a08d04SJames Feist                     }
336b7a08d04SJames Feist 
337b7a08d04SJames Feist                     // used for making maps out of 2 vectors
338b7a08d04SJames Feist                     const std::vector<double>* keys = nullptr;
339b7a08d04SJames Feist                     const std::vector<double>* values = nullptr;
340b7a08d04SJames Feist 
341b7a08d04SJames Feist                     for (const auto& propertyPair : intfPair.second)
342b7a08d04SJames Feist                     {
343b7a08d04SJames Feist                         if (propertyPair.first == "Type" ||
344b7a08d04SJames Feist                             propertyPair.first == "Class" ||
345b7a08d04SJames Feist                             propertyPair.first == "Name")
346b7a08d04SJames Feist                         {
347b7a08d04SJames Feist                             continue;
348b7a08d04SJames Feist                         }
349b7a08d04SJames Feist 
350b7a08d04SJames Feist                         // zones
351b7a08d04SJames Feist                         if (intfPair.first == pidZoneConfigurationIface)
352b7a08d04SJames Feist                         {
353b7a08d04SJames Feist                             const double* ptr =
354abf2add6SEd Tanous                                 std::get_if<double>(&propertyPair.second);
355b7a08d04SJames Feist                             if (ptr == nullptr)
356b7a08d04SJames Feist                             {
357b7a08d04SJames Feist                                 BMCWEB_LOG_ERROR << "Field Illegal "
358b7a08d04SJames Feist                                                  << propertyPair.first;
359b7a08d04SJames Feist                                 messages::internalError(asyncResp->res);
360b7a08d04SJames Feist                                 return;
361b7a08d04SJames Feist                             }
362b7a08d04SJames Feist                             (*config)[propertyPair.first] = *ptr;
363b7a08d04SJames Feist                         }
364b7a08d04SJames Feist 
365b7a08d04SJames Feist                         if (intfPair.first == stepwiseConfigurationIface)
366b7a08d04SJames Feist                         {
367b7a08d04SJames Feist                             if (propertyPair.first == "Reading" ||
368b7a08d04SJames Feist                                 propertyPair.first == "Output")
369b7a08d04SJames Feist                             {
370b7a08d04SJames Feist                                 const std::vector<double>* ptr =
371abf2add6SEd Tanous                                     std::get_if<std::vector<double>>(
372b7a08d04SJames Feist                                         &propertyPair.second);
373b7a08d04SJames Feist 
374b7a08d04SJames Feist                                 if (ptr == nullptr)
375b7a08d04SJames Feist                                 {
376b7a08d04SJames Feist                                     BMCWEB_LOG_ERROR << "Field Illegal "
377b7a08d04SJames Feist                                                      << propertyPair.first;
378b7a08d04SJames Feist                                     messages::internalError(asyncResp->res);
379b7a08d04SJames Feist                                     return;
380b7a08d04SJames Feist                                 }
381b7a08d04SJames Feist 
382b7a08d04SJames Feist                                 if (propertyPair.first == "Reading")
383b7a08d04SJames Feist                                 {
384b7a08d04SJames Feist                                     keys = ptr;
385b7a08d04SJames Feist                                 }
386b7a08d04SJames Feist                                 else
387b7a08d04SJames Feist                                 {
388b7a08d04SJames Feist                                     values = ptr;
389b7a08d04SJames Feist                                 }
390b7a08d04SJames Feist                                 if (keys && values)
391b7a08d04SJames Feist                                 {
392b7a08d04SJames Feist                                     if (keys->size() != values->size())
393b7a08d04SJames Feist                                     {
394b7a08d04SJames Feist                                         BMCWEB_LOG_ERROR
395b7a08d04SJames Feist                                             << "Reading and Output size don't "
396b7a08d04SJames Feist                                                "match ";
397b7a08d04SJames Feist                                         messages::internalError(asyncResp->res);
398b7a08d04SJames Feist                                         return;
399b7a08d04SJames Feist                                     }
400b7a08d04SJames Feist                                     nlohmann::json& steps = (*config)["Steps"];
401b7a08d04SJames Feist                                     steps = nlohmann::json::array();
402b7a08d04SJames Feist                                     for (size_t ii = 0; ii < keys->size(); ii++)
403b7a08d04SJames Feist                                     {
404b7a08d04SJames Feist                                         steps.push_back(
405b7a08d04SJames Feist                                             {{"Target", (*keys)[ii]},
406b7a08d04SJames Feist                                              {"Output", (*values)[ii]}});
407b7a08d04SJames Feist                                     }
408b7a08d04SJames Feist                                 }
409b7a08d04SJames Feist                             }
410b7a08d04SJames Feist                             if (propertyPair.first == "NegativeHysteresis" ||
411b7a08d04SJames Feist                                 propertyPair.first == "PositiveHysteresis")
412b7a08d04SJames Feist                             {
413b7a08d04SJames Feist                                 const double* ptr =
414abf2add6SEd Tanous                                     std::get_if<double>(&propertyPair.second);
415b7a08d04SJames Feist                                 if (ptr == nullptr)
416b7a08d04SJames Feist                                 {
417b7a08d04SJames Feist                                     BMCWEB_LOG_ERROR << "Field Illegal "
418b7a08d04SJames Feist                                                      << propertyPair.first;
419b7a08d04SJames Feist                                     messages::internalError(asyncResp->res);
420b7a08d04SJames Feist                                     return;
421b7a08d04SJames Feist                                 }
422b7a08d04SJames Feist                                 (*config)[propertyPair.first] = *ptr;
423b7a08d04SJames Feist                             }
424b7a08d04SJames Feist                         }
425b7a08d04SJames Feist 
426b7a08d04SJames Feist                         // pid and fans are off the same configuration
427b7a08d04SJames Feist                         if (intfPair.first == pidConfigurationIface ||
428b7a08d04SJames Feist                             intfPair.first == stepwiseConfigurationIface)
429b7a08d04SJames Feist                         {
4305b4aa86bSJames Feist 
4315b4aa86bSJames Feist                             if (propertyPair.first == "Zones")
4325b4aa86bSJames Feist                             {
4335b4aa86bSJames Feist                                 const std::vector<std::string>* inputs =
434abf2add6SEd Tanous                                     std::get_if<std::vector<std::string>>(
4351b6b96c5SEd Tanous                                         &propertyPair.second);
4365b4aa86bSJames Feist 
4375b4aa86bSJames Feist                                 if (inputs == nullptr)
4385b4aa86bSJames Feist                                 {
4395b4aa86bSJames Feist                                     BMCWEB_LOG_ERROR
4405b4aa86bSJames Feist                                         << "Zones Pid Field Illegal";
441a08b46ccSJason M. Bills                                     messages::internalError(asyncResp->res);
4425b4aa86bSJames Feist                                     return;
4435b4aa86bSJames Feist                                 }
444b7a08d04SJames Feist                                 auto& data = (*config)[propertyPair.first];
4455b4aa86bSJames Feist                                 data = nlohmann::json::array();
4465b4aa86bSJames Feist                                 for (std::string itemCopy : *inputs)
4475b4aa86bSJames Feist                                 {
4485b4aa86bSJames Feist                                     dbus::utility::escapePathForDbus(itemCopy);
4495b4aa86bSJames Feist                                     data.push_back(
4505b4aa86bSJames Feist                                         {{"@odata.id",
4515b4aa86bSJames Feist                                           "/redfish/v1/Managers/bmc#/Oem/"
4525b4aa86bSJames Feist                                           "OpenBmc/Fan/FanZones/" +
4535b4aa86bSJames Feist                                               itemCopy}});
4545b4aa86bSJames Feist                                 }
4555b4aa86bSJames Feist                             }
4565b4aa86bSJames Feist                             // todo(james): may never happen, but this
4575b4aa86bSJames Feist                             // assumes configuration data referenced in the
4585b4aa86bSJames Feist                             // PID config is provided by the same daemon, we
4595b4aa86bSJames Feist                             // could add another loop to cover all cases,
4605b4aa86bSJames Feist                             // but I'm okay kicking this can down the road a
4615b4aa86bSJames Feist                             // bit
4625b4aa86bSJames Feist 
4635b4aa86bSJames Feist                             else if (propertyPair.first == "Inputs" ||
4645b4aa86bSJames Feist                                      propertyPair.first == "Outputs")
4655b4aa86bSJames Feist                             {
466b7a08d04SJames Feist                                 auto& data = (*config)[propertyPair.first];
4675b4aa86bSJames Feist                                 const std::vector<std::string>* inputs =
468abf2add6SEd Tanous                                     std::get_if<std::vector<std::string>>(
4691b6b96c5SEd Tanous                                         &propertyPair.second);
4705b4aa86bSJames Feist 
4715b4aa86bSJames Feist                                 if (inputs == nullptr)
4725b4aa86bSJames Feist                                 {
4735b4aa86bSJames Feist                                     BMCWEB_LOG_ERROR << "Field Illegal "
4745b4aa86bSJames Feist                                                      << propertyPair.first;
475f12894f8SJason M. Bills                                     messages::internalError(asyncResp->res);
4765b4aa86bSJames Feist                                     return;
4775b4aa86bSJames Feist                                 }
4785b4aa86bSJames Feist                                 data = *inputs;
4795b4aa86bSJames Feist                             } // doubles
4805b4aa86bSJames Feist                             else if (propertyPair.first ==
4815b4aa86bSJames Feist                                          "FFGainCoefficient" ||
4825b4aa86bSJames Feist                                      propertyPair.first == "FFOffCoefficient" ||
4835b4aa86bSJames Feist                                      propertyPair.first == "ICoefficient" ||
4845b4aa86bSJames Feist                                      propertyPair.first == "ILimitMax" ||
4855b4aa86bSJames Feist                                      propertyPair.first == "ILimitMin" ||
486aad1a257SJames Feist                                      propertyPair.first ==
487aad1a257SJames Feist                                          "PositiveHysteresis" ||
488aad1a257SJames Feist                                      propertyPair.first ==
489aad1a257SJames Feist                                          "NegativeHysteresis" ||
4905b4aa86bSJames Feist                                      propertyPair.first == "OutLimitMax" ||
4915b4aa86bSJames Feist                                      propertyPair.first == "OutLimitMin" ||
4925b4aa86bSJames Feist                                      propertyPair.first == "PCoefficient" ||
4937625cb81SJames Feist                                      propertyPair.first == "SetPoint" ||
4945b4aa86bSJames Feist                                      propertyPair.first == "SlewNeg" ||
4955b4aa86bSJames Feist                                      propertyPair.first == "SlewPos")
4965b4aa86bSJames Feist                             {
4975b4aa86bSJames Feist                                 const double* ptr =
498abf2add6SEd Tanous                                     std::get_if<double>(&propertyPair.second);
4995b4aa86bSJames Feist                                 if (ptr == nullptr)
5005b4aa86bSJames Feist                                 {
5015b4aa86bSJames Feist                                     BMCWEB_LOG_ERROR << "Field Illegal "
5025b4aa86bSJames Feist                                                      << propertyPair.first;
503f12894f8SJason M. Bills                                     messages::internalError(asyncResp->res);
5045b4aa86bSJames Feist                                     return;
5055b4aa86bSJames Feist                                 }
506b7a08d04SJames Feist                                 (*config)[propertyPair.first] = *ptr;
5075b4aa86bSJames Feist                             }
5085b4aa86bSJames Feist                         }
5095b4aa86bSJames Feist                     }
5105b4aa86bSJames Feist                 }
5115b4aa86bSJames Feist             }
5125b4aa86bSJames Feist         },
5135b4aa86bSJames Feist         connection, path, objectManagerIface, "GetManagedObjects");
5145b4aa86bSJames Feist }
515ca537928SJennifer Lee 
51683ff9ab6SJames Feist enum class CreatePIDRet
51783ff9ab6SJames Feist {
51883ff9ab6SJames Feist     fail,
51983ff9ab6SJames Feist     del,
52083ff9ab6SJames Feist     patch
52183ff9ab6SJames Feist };
52283ff9ab6SJames Feist 
5235f2caaefSJames Feist static bool getZonesFromJsonReq(const std::shared_ptr<AsyncResp>& response,
5245f2caaefSJames Feist                                 std::vector<nlohmann::json>& config,
5255f2caaefSJames Feist                                 std::vector<std::string>& zones)
5265f2caaefSJames Feist {
527b6baeaa4SJames Feist     if (config.empty())
528b6baeaa4SJames Feist     {
529b6baeaa4SJames Feist         BMCWEB_LOG_ERROR << "Empty Zones";
530b6baeaa4SJames Feist         messages::propertyValueFormatError(response->res,
531b6baeaa4SJames Feist                                            nlohmann::json::array(), "Zones");
532b6baeaa4SJames Feist         return false;
533b6baeaa4SJames Feist     }
5345f2caaefSJames Feist     for (auto& odata : config)
5355f2caaefSJames Feist     {
5365f2caaefSJames Feist         std::string path;
5375f2caaefSJames Feist         if (!redfish::json_util::readJson(odata, response->res, "@odata.id",
5385f2caaefSJames Feist                                           path))
5395f2caaefSJames Feist         {
5405f2caaefSJames Feist             return false;
5415f2caaefSJames Feist         }
5425f2caaefSJames Feist         std::string input;
54361adbda3SJames Feist 
54461adbda3SJames Feist         // 8 below comes from
54561adbda3SJames Feist         // /redfish/v1/Managers/bmc#/Oem/OpenBmc/Fan/FanZones/Left
54661adbda3SJames Feist         //     0    1     2      3    4    5      6     7      8
54761adbda3SJames Feist         if (!dbus::utility::getNthStringFromPath(path, 8, input))
5485f2caaefSJames Feist         {
5495f2caaefSJames Feist             BMCWEB_LOG_ERROR << "Got invalid path " << path;
5505f2caaefSJames Feist             BMCWEB_LOG_ERROR << "Illegal Type Zones";
5515f2caaefSJames Feist             messages::propertyValueFormatError(response->res, odata.dump(),
5525f2caaefSJames Feist                                                "Zones");
5535f2caaefSJames Feist             return false;
5545f2caaefSJames Feist         }
5555f2caaefSJames Feist         boost::replace_all(input, "_", " ");
5565f2caaefSJames Feist         zones.emplace_back(std::move(input));
5575f2caaefSJames Feist     }
5585f2caaefSJames Feist     return true;
5595f2caaefSJames Feist }
5605f2caaefSJames Feist 
56173df0db0SJames Feist static const dbus::utility::ManagedItem*
56273df0db0SJames Feist     findChassis(const dbus::utility::ManagedObjectType& managedObj,
563b6baeaa4SJames Feist                 const std::string& value, std::string& chassis)
564b6baeaa4SJames Feist {
565b6baeaa4SJames Feist     BMCWEB_LOG_DEBUG << "Find Chassis: " << value << "\n";
566b6baeaa4SJames Feist 
567b6baeaa4SJames Feist     std::string escaped = boost::replace_all_copy(value, " ", "_");
568b6baeaa4SJames Feist     escaped = "/" + escaped;
569b6baeaa4SJames Feist     auto it = std::find_if(
570b6baeaa4SJames Feist         managedObj.begin(), managedObj.end(), [&escaped](const auto& obj) {
571b6baeaa4SJames Feist             if (boost::algorithm::ends_with(obj.first.str, escaped))
572b6baeaa4SJames Feist             {
573b6baeaa4SJames Feist                 BMCWEB_LOG_DEBUG << "Matched " << obj.first.str << "\n";
574b6baeaa4SJames Feist                 return true;
575b6baeaa4SJames Feist             }
576b6baeaa4SJames Feist             return false;
577b6baeaa4SJames Feist         });
578b6baeaa4SJames Feist 
579b6baeaa4SJames Feist     if (it == managedObj.end())
580b6baeaa4SJames Feist     {
58173df0db0SJames Feist         return nullptr;
582b6baeaa4SJames Feist     }
583b6baeaa4SJames Feist     // 5 comes from <chassis-name> being the 5th element
584b6baeaa4SJames Feist     // /xyz/openbmc_project/inventory/system/chassis/<chassis-name>
58573df0db0SJames Feist     if (dbus::utility::getNthStringFromPath(it->first.str, 5, chassis))
58673df0db0SJames Feist     {
58773df0db0SJames Feist         return &(*it);
58873df0db0SJames Feist     }
58973df0db0SJames Feist 
59073df0db0SJames Feist     return nullptr;
591b6baeaa4SJames Feist }
592b6baeaa4SJames Feist 
59383ff9ab6SJames Feist static CreatePIDRet createPidInterface(
59483ff9ab6SJames Feist     const std::shared_ptr<AsyncResp>& response, const std::string& type,
595b6baeaa4SJames Feist     nlohmann::json::iterator it, const std::string& path,
59683ff9ab6SJames Feist     const dbus::utility::ManagedObjectType& managedObj, bool createNewObject,
59783ff9ab6SJames Feist     boost::container::flat_map<std::string, dbus::utility::DbusVariantType>&
59883ff9ab6SJames Feist         output,
59973df0db0SJames Feist     std::string& chassis, const std::string& profile)
60083ff9ab6SJames Feist {
60183ff9ab6SJames Feist 
6025f2caaefSJames Feist     // common deleter
603b6baeaa4SJames Feist     if (it.value() == nullptr)
6045f2caaefSJames Feist     {
6055f2caaefSJames Feist         std::string iface;
6065f2caaefSJames Feist         if (type == "PidControllers" || type == "FanControllers")
6075f2caaefSJames Feist         {
6085f2caaefSJames Feist             iface = pidConfigurationIface;
6095f2caaefSJames Feist         }
6105f2caaefSJames Feist         else if (type == "FanZones")
6115f2caaefSJames Feist         {
6125f2caaefSJames Feist             iface = pidZoneConfigurationIface;
6135f2caaefSJames Feist         }
6145f2caaefSJames Feist         else if (type == "StepwiseControllers")
6155f2caaefSJames Feist         {
6165f2caaefSJames Feist             iface = stepwiseConfigurationIface;
6175f2caaefSJames Feist         }
6185f2caaefSJames Feist         else
6195f2caaefSJames Feist         {
6205f2caaefSJames Feist             BMCWEB_LOG_ERROR << "Line:" << __LINE__ << ", Illegal Type "
6215f2caaefSJames Feist                              << type;
6225f2caaefSJames Feist             messages::propertyUnknown(response->res, type);
6235f2caaefSJames Feist             return CreatePIDRet::fail;
6245f2caaefSJames Feist         }
6255f2caaefSJames Feist         // delete interface
6265f2caaefSJames Feist         crow::connections::systemBus->async_method_call(
6275f2caaefSJames Feist             [response, path](const boost::system::error_code ec) {
6285f2caaefSJames Feist                 if (ec)
6295f2caaefSJames Feist                 {
6305f2caaefSJames Feist                     BMCWEB_LOG_ERROR << "Error patching " << path << ": " << ec;
6315f2caaefSJames Feist                     messages::internalError(response->res);
632b6baeaa4SJames Feist                     return;
6335f2caaefSJames Feist                 }
634b6baeaa4SJames Feist                 messages::success(response->res);
6355f2caaefSJames Feist             },
6365f2caaefSJames Feist             "xyz.openbmc_project.EntityManager", path, iface, "Delete");
6375f2caaefSJames Feist         return CreatePIDRet::del;
6385f2caaefSJames Feist     }
6395f2caaefSJames Feist 
64073df0db0SJames Feist     const dbus::utility::ManagedItem* managedItem = nullptr;
641b6baeaa4SJames Feist     if (!createNewObject)
642b6baeaa4SJames Feist     {
643b6baeaa4SJames Feist         // if we aren't creating a new object, we should be able to find it on
644b6baeaa4SJames Feist         // d-bus
64573df0db0SJames Feist         managedItem = findChassis(managedObj, it.key(), chassis);
64673df0db0SJames Feist         if (managedItem == nullptr)
647b6baeaa4SJames Feist         {
648b6baeaa4SJames Feist             BMCWEB_LOG_ERROR << "Failed to get chassis from config patch";
649b6baeaa4SJames Feist             messages::invalidObject(response->res, it.key());
650b6baeaa4SJames Feist             return CreatePIDRet::fail;
651b6baeaa4SJames Feist         }
652b6baeaa4SJames Feist     }
653b6baeaa4SJames Feist 
65473df0db0SJames Feist     if (profile.size() &&
65573df0db0SJames Feist         (type == "PidControllers" || type == "FanControllers" ||
65673df0db0SJames Feist          type == "StepwiseControllers"))
65773df0db0SJames Feist     {
65873df0db0SJames Feist         if (managedItem == nullptr)
65973df0db0SJames Feist         {
66073df0db0SJames Feist             output["Profiles"] = std::vector<std::string>{profile};
66173df0db0SJames Feist         }
66273df0db0SJames Feist         else
66373df0db0SJames Feist         {
66473df0db0SJames Feist             std::string interface;
66573df0db0SJames Feist             if (type == "StepwiseControllers")
66673df0db0SJames Feist             {
66773df0db0SJames Feist                 interface = stepwiseConfigurationIface;
66873df0db0SJames Feist             }
66973df0db0SJames Feist             else
67073df0db0SJames Feist             {
67173df0db0SJames Feist                 interface = pidConfigurationIface;
67273df0db0SJames Feist             }
67373df0db0SJames Feist             auto findConfig = managedItem->second.find(interface);
67473df0db0SJames Feist             if (findConfig == managedItem->second.end())
67573df0db0SJames Feist             {
67673df0db0SJames Feist                 BMCWEB_LOG_ERROR
67773df0db0SJames Feist                     << "Failed to find interface in managed object";
67873df0db0SJames Feist                 messages::internalError(response->res);
67973df0db0SJames Feist                 return CreatePIDRet::fail;
68073df0db0SJames Feist             }
68173df0db0SJames Feist             auto findProfiles = findConfig->second.find("Profiles");
68273df0db0SJames Feist             if (findProfiles != findConfig->second.end())
68373df0db0SJames Feist             {
68473df0db0SJames Feist                 const std::vector<std::string>* curProfiles =
68573df0db0SJames Feist                     std::get_if<std::vector<std::string>>(
68673df0db0SJames Feist                         &(findProfiles->second));
68773df0db0SJames Feist                 if (curProfiles == nullptr)
68873df0db0SJames Feist                 {
68973df0db0SJames Feist                     BMCWEB_LOG_ERROR << "Illegal profiles in managed object";
69073df0db0SJames Feist                     messages::internalError(response->res);
69173df0db0SJames Feist                     return CreatePIDRet::fail;
69273df0db0SJames Feist                 }
69373df0db0SJames Feist                 if (std::find(curProfiles->begin(), curProfiles->end(),
69473df0db0SJames Feist                               profile) == curProfiles->end())
69573df0db0SJames Feist                 {
69673df0db0SJames Feist                     std::vector<std::string> newProfiles = *curProfiles;
69773df0db0SJames Feist                     newProfiles.push_back(profile);
69873df0db0SJames Feist                     output["Profiles"] = newProfiles;
69973df0db0SJames Feist                 }
70073df0db0SJames Feist             }
70173df0db0SJames Feist         }
70273df0db0SJames Feist     }
70373df0db0SJames Feist 
70483ff9ab6SJames Feist     if (type == "PidControllers" || type == "FanControllers")
70583ff9ab6SJames Feist     {
70683ff9ab6SJames Feist         if (createNewObject)
70783ff9ab6SJames Feist         {
70883ff9ab6SJames Feist             output["Class"] = type == "PidControllers" ? std::string("temp")
70983ff9ab6SJames Feist                                                        : std::string("fan");
71083ff9ab6SJames Feist             output["Type"] = std::string("Pid");
71183ff9ab6SJames Feist         }
7125f2caaefSJames Feist 
7135f2caaefSJames Feist         std::optional<std::vector<nlohmann::json>> zones;
7145f2caaefSJames Feist         std::optional<std::vector<std::string>> inputs;
7155f2caaefSJames Feist         std::optional<std::vector<std::string>> outputs;
7165f2caaefSJames Feist         std::map<std::string, std::optional<double>> doubles;
7175f2caaefSJames Feist         if (!redfish::json_util::readJson(
718b6baeaa4SJames Feist                 it.value(), response->res, "Inputs", inputs, "Outputs", outputs,
7195f2caaefSJames Feist                 "Zones", zones, "FFGainCoefficient",
7205f2caaefSJames Feist                 doubles["FFGainCoefficient"], "FFOffCoefficient",
7215f2caaefSJames Feist                 doubles["FFOffCoefficient"], "ICoefficient",
7225f2caaefSJames Feist                 doubles["ICoefficient"], "ILimitMax", doubles["ILimitMax"],
7235f2caaefSJames Feist                 "ILimitMin", doubles["ILimitMin"], "OutLimitMax",
7245f2caaefSJames Feist                 doubles["OutLimitMax"], "OutLimitMin", doubles["OutLimitMin"],
7255f2caaefSJames Feist                 "PCoefficient", doubles["PCoefficient"], "SetPoint",
7265f2caaefSJames Feist                 doubles["SetPoint"], "SlewNeg", doubles["SlewNeg"], "SlewPos",
727aad1a257SJames Feist                 doubles["SlewPos"], "PositiveHysteresis",
728aad1a257SJames Feist                 doubles["PositiveHysteresis"], "NegativeHysteresis",
729aad1a257SJames Feist                 doubles["NegativeHysteresis"]))
73083ff9ab6SJames Feist         {
7315f2caaefSJames Feist             BMCWEB_LOG_ERROR << "Line:" << __LINE__ << ", Illegal Property "
732b6baeaa4SJames Feist                              << it.value().dump();
7335f2caaefSJames Feist             return CreatePIDRet::fail;
73483ff9ab6SJames Feist         }
7355f2caaefSJames Feist         if (zones)
7365f2caaefSJames Feist         {
7375f2caaefSJames Feist             std::vector<std::string> zonesStr;
7385f2caaefSJames Feist             if (!getZonesFromJsonReq(response, *zones, zonesStr))
7395f2caaefSJames Feist             {
7405f2caaefSJames Feist                 BMCWEB_LOG_ERROR << "Line:" << __LINE__ << ", Illegal Zones";
7415f2caaefSJames Feist                 return CreatePIDRet::fail;
7425f2caaefSJames Feist             }
743b6baeaa4SJames Feist             if (chassis.empty() &&
744b6baeaa4SJames Feist                 !findChassis(managedObj, zonesStr[0], chassis))
745b6baeaa4SJames Feist             {
746b6baeaa4SJames Feist                 BMCWEB_LOG_ERROR << "Failed to get chassis from config patch";
747b6baeaa4SJames Feist                 messages::invalidObject(response->res, it.key());
748b6baeaa4SJames Feist                 return CreatePIDRet::fail;
749b6baeaa4SJames Feist             }
750b6baeaa4SJames Feist 
7515f2caaefSJames Feist             output["Zones"] = std::move(zonesStr);
7525f2caaefSJames Feist         }
7535f2caaefSJames Feist         if (inputs || outputs)
7545f2caaefSJames Feist         {
7555f2caaefSJames Feist             std::array<std::optional<std::vector<std::string>>*, 2> containers =
7565f2caaefSJames Feist                 {&inputs, &outputs};
7575f2caaefSJames Feist             size_t index = 0;
7585f2caaefSJames Feist             for (const auto& containerPtr : containers)
7595f2caaefSJames Feist             {
7605f2caaefSJames Feist                 std::optional<std::vector<std::string>>& container =
7615f2caaefSJames Feist                     *containerPtr;
7625f2caaefSJames Feist                 if (!container)
7635f2caaefSJames Feist                 {
7645f2caaefSJames Feist                     index++;
7655f2caaefSJames Feist                     continue;
76683ff9ab6SJames Feist                 }
76783ff9ab6SJames Feist 
7685f2caaefSJames Feist                 for (std::string& value : *container)
76983ff9ab6SJames Feist                 {
7705f2caaefSJames Feist                     boost::replace_all(value, "_", " ");
77183ff9ab6SJames Feist                 }
7725f2caaefSJames Feist                 std::string key;
7735f2caaefSJames Feist                 if (index == 0)
7745f2caaefSJames Feist                 {
7755f2caaefSJames Feist                     key = "Inputs";
7765f2caaefSJames Feist                 }
7775f2caaefSJames Feist                 else
7785f2caaefSJames Feist                 {
7795f2caaefSJames Feist                     key = "Outputs";
7805f2caaefSJames Feist                 }
7815f2caaefSJames Feist                 output[key] = *container;
7825f2caaefSJames Feist                 index++;
7835f2caaefSJames Feist             }
78483ff9ab6SJames Feist         }
78583ff9ab6SJames Feist 
78683ff9ab6SJames Feist         // doubles
7875f2caaefSJames Feist         for (const auto& pairs : doubles)
78883ff9ab6SJames Feist         {
7895f2caaefSJames Feist             if (!pairs.second)
79083ff9ab6SJames Feist             {
7915f2caaefSJames Feist                 continue;
79283ff9ab6SJames Feist             }
7935f2caaefSJames Feist             BMCWEB_LOG_DEBUG << pairs.first << " = " << *pairs.second;
7945f2caaefSJames Feist             output[pairs.first] = *(pairs.second);
7955f2caaefSJames Feist         }
79683ff9ab6SJames Feist     }
79783ff9ab6SJames Feist 
79883ff9ab6SJames Feist     else if (type == "FanZones")
79983ff9ab6SJames Feist     {
80083ff9ab6SJames Feist         output["Type"] = std::string("Pid.Zone");
80183ff9ab6SJames Feist 
8025f2caaefSJames Feist         std::optional<nlohmann::json> chassisContainer;
8035f2caaefSJames Feist         std::optional<double> failSafePercent;
804d3ec07f8SJames Feist         std::optional<double> minThermalOutput;
805b6baeaa4SJames Feist         if (!redfish::json_util::readJson(it.value(), response->res, "Chassis",
8065f2caaefSJames Feist                                           chassisContainer, "FailSafePercent",
807d3ec07f8SJames Feist                                           failSafePercent, "MinThermalOutput",
808d3ec07f8SJames Feist                                           minThermalOutput))
80983ff9ab6SJames Feist         {
8105f2caaefSJames Feist             BMCWEB_LOG_ERROR << "Line:" << __LINE__ << ", Illegal Property "
811b6baeaa4SJames Feist                              << it.value().dump();
81283ff9ab6SJames Feist             return CreatePIDRet::fail;
81383ff9ab6SJames Feist         }
8145f2caaefSJames Feist 
8155f2caaefSJames Feist         if (chassisContainer)
81683ff9ab6SJames Feist         {
8175f2caaefSJames Feist 
8185f2caaefSJames Feist             std::string chassisId;
8195f2caaefSJames Feist             if (!redfish::json_util::readJson(*chassisContainer, response->res,
8205f2caaefSJames Feist                                               "@odata.id", chassisId))
8215f2caaefSJames Feist             {
8225f2caaefSJames Feist                 BMCWEB_LOG_ERROR << "Line:" << __LINE__ << ", Illegal Property "
8235f2caaefSJames Feist                                  << chassisContainer->dump();
82483ff9ab6SJames Feist                 return CreatePIDRet::fail;
82583ff9ab6SJames Feist             }
82683ff9ab6SJames Feist 
82783ff9ab6SJames Feist             // /refish/v1/chassis/chassis_name/
8285f2caaefSJames Feist             if (!dbus::utility::getNthStringFromPath(chassisId, 3, chassis))
82983ff9ab6SJames Feist             {
8305f2caaefSJames Feist                 BMCWEB_LOG_ERROR << "Got invalid path " << chassisId;
8315f2caaefSJames Feist                 messages::invalidObject(response->res, chassisId);
83283ff9ab6SJames Feist                 return CreatePIDRet::fail;
83383ff9ab6SJames Feist             }
83483ff9ab6SJames Feist         }
835d3ec07f8SJames Feist         if (minThermalOutput)
83683ff9ab6SJames Feist         {
837d3ec07f8SJames Feist             output["MinThermalOutput"] = *minThermalOutput;
8385f2caaefSJames Feist         }
8395f2caaefSJames Feist         if (failSafePercent)
84083ff9ab6SJames Feist         {
8415f2caaefSJames Feist             output["FailSafePercent"] = *failSafePercent;
8425f2caaefSJames Feist         }
8435f2caaefSJames Feist     }
8445f2caaefSJames Feist     else if (type == "StepwiseControllers")
8455f2caaefSJames Feist     {
8465f2caaefSJames Feist         output["Type"] = std::string("Stepwise");
8475f2caaefSJames Feist 
8485f2caaefSJames Feist         std::optional<std::vector<nlohmann::json>> zones;
8495f2caaefSJames Feist         std::optional<std::vector<nlohmann::json>> steps;
8505f2caaefSJames Feist         std::optional<std::vector<std::string>> inputs;
8515f2caaefSJames Feist         std::optional<double> positiveHysteresis;
8525f2caaefSJames Feist         std::optional<double> negativeHysteresis;
853c33a90ecSJames Feist         std::optional<std::string> direction; // upper clipping curve vs lower
8545f2caaefSJames Feist         if (!redfish::json_util::readJson(
855b6baeaa4SJames Feist                 it.value(), response->res, "Zones", zones, "Steps", steps,
856b6baeaa4SJames Feist                 "Inputs", inputs, "PositiveHysteresis", positiveHysteresis,
857c33a90ecSJames Feist                 "NegativeHysteresis", negativeHysteresis, "Direction",
858c33a90ecSJames Feist                 direction))
8595f2caaefSJames Feist         {
8605f2caaefSJames Feist             BMCWEB_LOG_ERROR << "Line:" << __LINE__ << ", Illegal Property "
861b6baeaa4SJames Feist                              << it.value().dump();
86283ff9ab6SJames Feist             return CreatePIDRet::fail;
86383ff9ab6SJames Feist         }
8645f2caaefSJames Feist 
8655f2caaefSJames Feist         if (zones)
86683ff9ab6SJames Feist         {
867b6baeaa4SJames Feist             std::vector<std::string> zonesStrs;
868b6baeaa4SJames Feist             if (!getZonesFromJsonReq(response, *zones, zonesStrs))
8695f2caaefSJames Feist             {
8705f2caaefSJames Feist                 BMCWEB_LOG_ERROR << "Line:" << __LINE__ << ", Illegal Zones";
87183ff9ab6SJames Feist                 return CreatePIDRet::fail;
87283ff9ab6SJames Feist             }
873b6baeaa4SJames Feist             if (chassis.empty() &&
874b6baeaa4SJames Feist                 !findChassis(managedObj, zonesStrs[0], chassis))
875b6baeaa4SJames Feist             {
876b6baeaa4SJames Feist                 BMCWEB_LOG_ERROR << "Failed to get chassis from config patch";
877b6baeaa4SJames Feist                 messages::invalidObject(response->res, it.key());
878b6baeaa4SJames Feist                 return CreatePIDRet::fail;
879b6baeaa4SJames Feist             }
880b6baeaa4SJames Feist             output["Zones"] = std::move(zonesStrs);
8815f2caaefSJames Feist         }
8825f2caaefSJames Feist         if (steps)
8835f2caaefSJames Feist         {
8845f2caaefSJames Feist             std::vector<double> readings;
8855f2caaefSJames Feist             std::vector<double> outputs;
8865f2caaefSJames Feist             for (auto& step : *steps)
8875f2caaefSJames Feist             {
8885f2caaefSJames Feist                 double target;
889b01bf299SEd Tanous                 double output;
8905f2caaefSJames Feist 
8915f2caaefSJames Feist                 if (!redfish::json_util::readJson(step, response->res, "Target",
892b01bf299SEd Tanous                                                   target, "Output", output))
8935f2caaefSJames Feist                 {
8945f2caaefSJames Feist                     BMCWEB_LOG_ERROR << "Line:" << __LINE__
895b6baeaa4SJames Feist                                      << ", Illegal Property "
896b6baeaa4SJames Feist                                      << it.value().dump();
8975f2caaefSJames Feist                     return CreatePIDRet::fail;
8985f2caaefSJames Feist                 }
8995f2caaefSJames Feist                 readings.emplace_back(target);
900b01bf299SEd Tanous                 outputs.emplace_back(output);
9015f2caaefSJames Feist             }
9025f2caaefSJames Feist             output["Reading"] = std::move(readings);
9035f2caaefSJames Feist             output["Output"] = std::move(outputs);
9045f2caaefSJames Feist         }
9055f2caaefSJames Feist         if (inputs)
9065f2caaefSJames Feist         {
9075f2caaefSJames Feist             for (std::string& value : *inputs)
9085f2caaefSJames Feist             {
9095f2caaefSJames Feist                 boost::replace_all(value, "_", " ");
9105f2caaefSJames Feist             }
9115f2caaefSJames Feist             output["Inputs"] = std::move(*inputs);
9125f2caaefSJames Feist         }
9135f2caaefSJames Feist         if (negativeHysteresis)
9145f2caaefSJames Feist         {
9155f2caaefSJames Feist             output["NegativeHysteresis"] = *negativeHysteresis;
9165f2caaefSJames Feist         }
9175f2caaefSJames Feist         if (positiveHysteresis)
9185f2caaefSJames Feist         {
9195f2caaefSJames Feist             output["PositiveHysteresis"] = *positiveHysteresis;
92083ff9ab6SJames Feist         }
921c33a90ecSJames Feist         if (direction)
922c33a90ecSJames Feist         {
923c33a90ecSJames Feist             constexpr const std::array<const char*, 2> allowedDirections = {
924c33a90ecSJames Feist                 "Ceiling", "Floor"};
925c33a90ecSJames Feist             if (std::find(allowedDirections.begin(), allowedDirections.end(),
926c33a90ecSJames Feist                           *direction) == allowedDirections.end())
927c33a90ecSJames Feist             {
928c33a90ecSJames Feist                 messages::propertyValueTypeError(response->res, "Direction",
929c33a90ecSJames Feist                                                  *direction);
930c33a90ecSJames Feist                 return CreatePIDRet::fail;
931c33a90ecSJames Feist             }
932c33a90ecSJames Feist             output["Class"] = *direction;
933c33a90ecSJames Feist         }
93483ff9ab6SJames Feist     }
93583ff9ab6SJames Feist     else
93683ff9ab6SJames Feist     {
9375f2caaefSJames Feist         BMCWEB_LOG_ERROR << "Line:" << __LINE__ << ", Illegal Type " << type;
93835a62c7cSJason M. Bills         messages::propertyUnknown(response->res, type);
93983ff9ab6SJames Feist         return CreatePIDRet::fail;
94083ff9ab6SJames Feist     }
94183ff9ab6SJames Feist     return CreatePIDRet::patch;
94283ff9ab6SJames Feist }
94373df0db0SJames Feist struct GetPIDValues : std::enable_shared_from_this<GetPIDValues>
94473df0db0SJames Feist {
94583ff9ab6SJames Feist 
94673df0db0SJames Feist     GetPIDValues(const std::shared_ptr<AsyncResp>& asyncResp) :
94773df0db0SJames Feist         asyncResp(asyncResp)
94873df0db0SJames Feist 
9491abe55efSEd Tanous     {
9509c310685SBorawski.Lukasz     }
9519c310685SBorawski.Lukasz 
95273df0db0SJames Feist     void run()
9535b4aa86bSJames Feist     {
95473df0db0SJames Feist         std::shared_ptr<GetPIDValues> self = shared_from_this();
95573df0db0SJames Feist 
95673df0db0SJames Feist         // get all configurations
9575b4aa86bSJames Feist         crow::connections::systemBus->async_method_call(
95873df0db0SJames Feist             [self](const boost::system::error_code ec,
9595b4aa86bSJames Feist                    const crow::openbmc_mapper::GetSubTreeType& subtree) {
9605b4aa86bSJames Feist                 if (ec)
9615b4aa86bSJames Feist                 {
9625b4aa86bSJames Feist                     BMCWEB_LOG_ERROR << ec;
96373df0db0SJames Feist                     messages::internalError(self->asyncResp->res);
96473df0db0SJames Feist                     return;
96573df0db0SJames Feist                 }
96673df0db0SJames Feist                 self->subtree = subtree;
96773df0db0SJames Feist             },
96873df0db0SJames Feist             "xyz.openbmc_project.ObjectMapper",
96973df0db0SJames Feist             "/xyz/openbmc_project/object_mapper",
97073df0db0SJames Feist             "xyz.openbmc_project.ObjectMapper", "GetSubTree", "/", 0,
97173df0db0SJames Feist             std::array<const char*, 4>{
97273df0db0SJames Feist                 pidConfigurationIface, pidZoneConfigurationIface,
97373df0db0SJames Feist                 objectManagerIface, stepwiseConfigurationIface});
97473df0db0SJames Feist 
97573df0db0SJames Feist         // at the same time get the selected profile
97673df0db0SJames Feist         crow::connections::systemBus->async_method_call(
97773df0db0SJames Feist             [self](const boost::system::error_code ec,
97873df0db0SJames Feist                    const crow::openbmc_mapper::GetSubTreeType& subtree) {
97973df0db0SJames Feist                 if (ec || subtree.empty())
98073df0db0SJames Feist                 {
98173df0db0SJames Feist                     return;
98273df0db0SJames Feist                 }
98373df0db0SJames Feist                 if (subtree[0].second.size() != 1)
98473df0db0SJames Feist                 {
98573df0db0SJames Feist                     // invalid mapper response, should never happen
98673df0db0SJames Feist                     BMCWEB_LOG_ERROR << "GetPIDValues: Mapper Error";
98773df0db0SJames Feist                     messages::internalError(self->asyncResp->res);
9885b4aa86bSJames Feist                     return;
9895b4aa86bSJames Feist                 }
9905b4aa86bSJames Feist 
99173df0db0SJames Feist                 const std::string& path = subtree[0].first;
99273df0db0SJames Feist                 const std::string& owner = subtree[0].second[0].first;
99373df0db0SJames Feist                 crow::connections::systemBus->async_method_call(
99473df0db0SJames Feist                     [path, owner, self](
99573df0db0SJames Feist                         const boost::system::error_code ec,
99673df0db0SJames Feist                         const boost::container::flat_map<
99773df0db0SJames Feist                             std::string, std::variant<std::vector<std::string>,
99873df0db0SJames Feist                                                       std::string>>& resp) {
99973df0db0SJames Feist                         if (ec)
100073df0db0SJames Feist                         {
100173df0db0SJames Feist                             BMCWEB_LOG_ERROR << "GetPIDValues: Can't get "
100273df0db0SJames Feist                                                 "thermalModeIface "
100373df0db0SJames Feist                                              << path;
100473df0db0SJames Feist                             messages::internalError(self->asyncResp->res);
100573df0db0SJames Feist                             return;
100673df0db0SJames Feist                         }
100773df0db0SJames Feist                         const std::string* current;
100873df0db0SJames Feist                         const std::vector<std::string>* supported;
100973df0db0SJames Feist                         for (auto& [key, value] : resp)
101073df0db0SJames Feist                         {
101173df0db0SJames Feist                             if (key == "Current")
101273df0db0SJames Feist                             {
101373df0db0SJames Feist                                 current = std::get_if<std::string>(&value);
101473df0db0SJames Feist                                 if (current == nullptr)
101573df0db0SJames Feist                                 {
101673df0db0SJames Feist                                     BMCWEB_LOG_ERROR
101773df0db0SJames Feist                                         << "GetPIDValues: thermal mode "
101873df0db0SJames Feist                                            "iface invalid "
101973df0db0SJames Feist                                         << path;
102073df0db0SJames Feist                                     messages::internalError(
102173df0db0SJames Feist                                         self->asyncResp->res);
102273df0db0SJames Feist                                     return;
102373df0db0SJames Feist                                 }
102473df0db0SJames Feist                             }
102573df0db0SJames Feist                             if (key == "Supported")
102673df0db0SJames Feist                             {
102773df0db0SJames Feist                                 supported =
102873df0db0SJames Feist                                     std::get_if<std::vector<std::string>>(
102973df0db0SJames Feist                                         &value);
103073df0db0SJames Feist                                 if (supported == nullptr)
103173df0db0SJames Feist                                 {
103273df0db0SJames Feist                                     BMCWEB_LOG_ERROR
103373df0db0SJames Feist                                         << "GetPIDValues: thermal mode "
103473df0db0SJames Feist                                            "iface invalid"
103573df0db0SJames Feist                                         << path;
103673df0db0SJames Feist                                     messages::internalError(
103773df0db0SJames Feist                                         self->asyncResp->res);
103873df0db0SJames Feist                                     return;
103973df0db0SJames Feist                                 }
104073df0db0SJames Feist                             }
104173df0db0SJames Feist                         }
104273df0db0SJames Feist                         if (current == nullptr || supported == nullptr)
104373df0db0SJames Feist                         {
104473df0db0SJames Feist                             BMCWEB_LOG_ERROR << "GetPIDValues: thermal mode "
104573df0db0SJames Feist                                                 "iface invalid "
104673df0db0SJames Feist                                              << path;
104773df0db0SJames Feist                             messages::internalError(self->asyncResp->res);
104873df0db0SJames Feist                             return;
104973df0db0SJames Feist                         }
105073df0db0SJames Feist                         self->currentProfile = *current;
105173df0db0SJames Feist                         self->supportedProfiles = *supported;
105273df0db0SJames Feist                     },
105373df0db0SJames Feist                     owner, path, "org.freedesktop.DBus.Properties", "GetAll",
105473df0db0SJames Feist                     thermalModeIface);
105573df0db0SJames Feist             },
105673df0db0SJames Feist             "xyz.openbmc_project.ObjectMapper",
105773df0db0SJames Feist             "/xyz/openbmc_project/object_mapper",
105873df0db0SJames Feist             "xyz.openbmc_project.ObjectMapper", "GetSubTree", "/", 0,
105973df0db0SJames Feist             std::array<const char*, 1>{thermalModeIface});
106073df0db0SJames Feist     }
106173df0db0SJames Feist 
106273df0db0SJames Feist     ~GetPIDValues()
106373df0db0SJames Feist     {
106473df0db0SJames Feist         if (asyncResp->res.result() != boost::beast::http::status::ok)
106573df0db0SJames Feist         {
106673df0db0SJames Feist             return;
106773df0db0SJames Feist         }
10685b4aa86bSJames Feist         // create map of <connection, path to objMgr>>
106973df0db0SJames Feist         boost::container::flat_map<std::string, std::string> objectMgrPaths;
10706bce33bcSJames Feist         boost::container::flat_set<std::string> calledConnections;
10715b4aa86bSJames Feist         for (const auto& pathGroup : subtree)
10725b4aa86bSJames Feist         {
10735b4aa86bSJames Feist             for (const auto& connectionGroup : pathGroup.second)
10745b4aa86bSJames Feist             {
10756bce33bcSJames Feist                 auto findConnection =
10766bce33bcSJames Feist                     calledConnections.find(connectionGroup.first);
10776bce33bcSJames Feist                 if (findConnection != calledConnections.end())
10786bce33bcSJames Feist                 {
10796bce33bcSJames Feist                     break;
10806bce33bcSJames Feist                 }
108173df0db0SJames Feist                 for (const std::string& interface : connectionGroup.second)
10825b4aa86bSJames Feist                 {
10835b4aa86bSJames Feist                     if (interface == objectManagerIface)
10845b4aa86bSJames Feist                     {
108573df0db0SJames Feist                         objectMgrPaths[connectionGroup.first] = pathGroup.first;
10865b4aa86bSJames Feist                     }
10875b4aa86bSJames Feist                     // this list is alphabetical, so we
10885b4aa86bSJames Feist                     // should have found the objMgr by now
10895b4aa86bSJames Feist                     if (interface == pidConfigurationIface ||
1090b7a08d04SJames Feist                         interface == pidZoneConfigurationIface ||
1091b7a08d04SJames Feist                         interface == stepwiseConfigurationIface)
10925b4aa86bSJames Feist                     {
10935b4aa86bSJames Feist                         auto findObjMgr =
10945b4aa86bSJames Feist                             objectMgrPaths.find(connectionGroup.first);
10955b4aa86bSJames Feist                         if (findObjMgr == objectMgrPaths.end())
10965b4aa86bSJames Feist                         {
10975b4aa86bSJames Feist                             BMCWEB_LOG_DEBUG << connectionGroup.first
10985b4aa86bSJames Feist                                              << "Has no Object Manager";
10995b4aa86bSJames Feist                             continue;
11005b4aa86bSJames Feist                         }
11016bce33bcSJames Feist 
11026bce33bcSJames Feist                         calledConnections.insert(connectionGroup.first);
11036bce33bcSJames Feist 
110473df0db0SJames Feist                         asyncPopulatePid(findObjMgr->first, findObjMgr->second,
110573df0db0SJames Feist                                          currentProfile, supportedProfiles,
110673df0db0SJames Feist                                          asyncResp);
11075b4aa86bSJames Feist                         break;
11085b4aa86bSJames Feist                     }
11095b4aa86bSJames Feist                 }
11105b4aa86bSJames Feist             }
11115b4aa86bSJames Feist         }
111273df0db0SJames Feist     }
111373df0db0SJames Feist 
111473df0db0SJames Feist     std::vector<std::string> supportedProfiles;
111573df0db0SJames Feist     std::string currentProfile;
111673df0db0SJames Feist     crow::openbmc_mapper::GetSubTreeType subtree;
111773df0db0SJames Feist     std::shared_ptr<AsyncResp> asyncResp;
111873df0db0SJames Feist };
111973df0db0SJames Feist 
112073df0db0SJames Feist struct SetPIDValues : std::enable_shared_from_this<SetPIDValues>
112173df0db0SJames Feist {
112273df0db0SJames Feist 
112373df0db0SJames Feist     SetPIDValues(const std::shared_ptr<AsyncResp>& asyncResp,
112473df0db0SJames Feist                  nlohmann::json& data) :
112573df0db0SJames Feist         asyncResp(asyncResp)
112673df0db0SJames Feist     {
112773df0db0SJames Feist 
112873df0db0SJames Feist         std::optional<nlohmann::json> pidControllers;
112973df0db0SJames Feist         std::optional<nlohmann::json> fanControllers;
113073df0db0SJames Feist         std::optional<nlohmann::json> fanZones;
113173df0db0SJames Feist         std::optional<nlohmann::json> stepwiseControllers;
113273df0db0SJames Feist 
113373df0db0SJames Feist         if (!redfish::json_util::readJson(
113473df0db0SJames Feist                 data, asyncResp->res, "PidControllers", pidControllers,
113573df0db0SJames Feist                 "FanControllers", fanControllers, "FanZones", fanZones,
113673df0db0SJames Feist                 "StepwiseControllers", stepwiseControllers, "Profile", profile))
113773df0db0SJames Feist         {
113873df0db0SJames Feist             BMCWEB_LOG_ERROR << "Line:" << __LINE__ << ", Illegal Property "
113973df0db0SJames Feist                              << data.dump();
114073df0db0SJames Feist             return;
114173df0db0SJames Feist         }
114273df0db0SJames Feist         configuration.emplace_back("PidControllers", std::move(pidControllers));
114373df0db0SJames Feist         configuration.emplace_back("FanControllers", std::move(fanControllers));
114473df0db0SJames Feist         configuration.emplace_back("FanZones", std::move(fanZones));
114573df0db0SJames Feist         configuration.emplace_back("StepwiseControllers",
114673df0db0SJames Feist                                    std::move(stepwiseControllers));
114773df0db0SJames Feist     }
114873df0db0SJames Feist     void run()
114973df0db0SJames Feist     {
115073df0db0SJames Feist         if (asyncResp->res.result() != boost::beast::http::status::ok)
115173df0db0SJames Feist         {
115273df0db0SJames Feist             return;
115373df0db0SJames Feist         }
115473df0db0SJames Feist 
115573df0db0SJames Feist         std::shared_ptr<SetPIDValues> self = shared_from_this();
115673df0db0SJames Feist 
115773df0db0SJames Feist         // todo(james): might make sense to do a mapper call here if this
115873df0db0SJames Feist         // interface gets more traction
115973df0db0SJames Feist         crow::connections::systemBus->async_method_call(
116073df0db0SJames Feist             [self](const boost::system::error_code ec,
116173df0db0SJames Feist                    dbus::utility::ManagedObjectType& managedObj) {
116273df0db0SJames Feist                 if (ec)
116373df0db0SJames Feist                 {
116473df0db0SJames Feist                     BMCWEB_LOG_ERROR << "Error communicating to Entity Manager";
116573df0db0SJames Feist                     messages::internalError(self->asyncResp->res);
116673df0db0SJames Feist                     return;
116773df0db0SJames Feist                 }
116873df0db0SJames Feist                 self->managedObj = std::move(managedObj);
116973df0db0SJames Feist             },
117073df0db0SJames Feist             "xyz.openbmc_project.EntityManager", "/", objectManagerIface,
117173df0db0SJames Feist             "GetManagedObjects");
117273df0db0SJames Feist 
117373df0db0SJames Feist         // at the same time get the profile information
117473df0db0SJames Feist         crow::connections::systemBus->async_method_call(
117573df0db0SJames Feist             [self](const boost::system::error_code ec,
117673df0db0SJames Feist                    const crow::openbmc_mapper::GetSubTreeType& subtree) {
117773df0db0SJames Feist                 if (ec || subtree.empty())
117873df0db0SJames Feist                 {
117973df0db0SJames Feist                     return;
118073df0db0SJames Feist                 }
118173df0db0SJames Feist                 if (subtree[0].second.empty())
118273df0db0SJames Feist                 {
118373df0db0SJames Feist                     // invalid mapper response, should never happen
118473df0db0SJames Feist                     BMCWEB_LOG_ERROR << "SetPIDValues: Mapper Error";
118573df0db0SJames Feist                     messages::internalError(self->asyncResp->res);
118673df0db0SJames Feist                     return;
118773df0db0SJames Feist                 }
118873df0db0SJames Feist 
118973df0db0SJames Feist                 const std::string& path = subtree[0].first;
119073df0db0SJames Feist                 const std::string& owner = subtree[0].second[0].first;
119173df0db0SJames Feist                 crow::connections::systemBus->async_method_call(
119273df0db0SJames Feist                     [self, path, owner](
119373df0db0SJames Feist                         const boost::system::error_code ec,
119473df0db0SJames Feist                         const boost::container::flat_map<
119573df0db0SJames Feist                             std::string, std::variant<std::vector<std::string>,
119673df0db0SJames Feist                                                       std::string>>& resp) {
119773df0db0SJames Feist                         if (ec)
119873df0db0SJames Feist                         {
119973df0db0SJames Feist                             BMCWEB_LOG_ERROR << "SetPIDValues: Can't get "
120073df0db0SJames Feist                                                 "thermalModeIface "
120173df0db0SJames Feist                                              << path;
120273df0db0SJames Feist                             messages::internalError(self->asyncResp->res);
120373df0db0SJames Feist                             return;
120473df0db0SJames Feist                         }
120573df0db0SJames Feist                         const std::string* current;
120673df0db0SJames Feist                         const std::vector<std::string>* supported;
120773df0db0SJames Feist                         for (auto& [key, value] : resp)
120873df0db0SJames Feist                         {
120973df0db0SJames Feist                             if (key == "Current")
121073df0db0SJames Feist                             {
121173df0db0SJames Feist                                 current = std::get_if<std::string>(&value);
121273df0db0SJames Feist                                 if (current == nullptr)
121373df0db0SJames Feist                                 {
121473df0db0SJames Feist                                     BMCWEB_LOG_ERROR
121573df0db0SJames Feist                                         << "SetPIDValues: thermal mode "
121673df0db0SJames Feist                                            "iface invalid "
121773df0db0SJames Feist                                         << path;
121873df0db0SJames Feist                                     messages::internalError(
121973df0db0SJames Feist                                         self->asyncResp->res);
122073df0db0SJames Feist                                     return;
122173df0db0SJames Feist                                 }
122273df0db0SJames Feist                             }
122373df0db0SJames Feist                             if (key == "Supported")
122473df0db0SJames Feist                             {
122573df0db0SJames Feist                                 supported =
122673df0db0SJames Feist                                     std::get_if<std::vector<std::string>>(
122773df0db0SJames Feist                                         &value);
122873df0db0SJames Feist                                 if (supported == nullptr)
122973df0db0SJames Feist                                 {
123073df0db0SJames Feist                                     BMCWEB_LOG_ERROR
123173df0db0SJames Feist                                         << "SetPIDValues: thermal mode "
123273df0db0SJames Feist                                            "iface invalid"
123373df0db0SJames Feist                                         << path;
123473df0db0SJames Feist                                     messages::internalError(
123573df0db0SJames Feist                                         self->asyncResp->res);
123673df0db0SJames Feist                                     return;
123773df0db0SJames Feist                                 }
123873df0db0SJames Feist                             }
123973df0db0SJames Feist                         }
124073df0db0SJames Feist                         if (current == nullptr || supported == nullptr)
124173df0db0SJames Feist                         {
124273df0db0SJames Feist                             BMCWEB_LOG_ERROR << "SetPIDValues: thermal mode "
124373df0db0SJames Feist                                                 "iface invalid "
124473df0db0SJames Feist                                              << path;
124573df0db0SJames Feist                             messages::internalError(self->asyncResp->res);
124673df0db0SJames Feist                             return;
124773df0db0SJames Feist                         }
124873df0db0SJames Feist                         self->currentProfile = *current;
124973df0db0SJames Feist                         self->supportedProfiles = *supported;
125073df0db0SJames Feist                         self->profileConnection = owner;
125173df0db0SJames Feist                         self->profilePath = path;
125273df0db0SJames Feist                     },
125373df0db0SJames Feist                     owner, path, "org.freedesktop.DBus.Properties", "GetAll",
125473df0db0SJames Feist                     thermalModeIface);
12555b4aa86bSJames Feist             },
12565b4aa86bSJames Feist             "xyz.openbmc_project.ObjectMapper",
12575b4aa86bSJames Feist             "/xyz/openbmc_project/object_mapper",
12585b4aa86bSJames Feist             "xyz.openbmc_project.ObjectMapper", "GetSubTree", "/", 0,
125973df0db0SJames Feist             std::array<const char*, 1>{thermalModeIface});
126073df0db0SJames Feist     }
126173df0db0SJames Feist     ~SetPIDValues()
126273df0db0SJames Feist     {
126373df0db0SJames Feist         if (asyncResp->res.result() != boost::beast::http::status::ok)
126473df0db0SJames Feist         {
126573df0db0SJames Feist             return;
12665b4aa86bSJames Feist         }
12675b4aa86bSJames Feist 
126873df0db0SJames Feist         std::shared_ptr<AsyncResp> response = asyncResp;
126973df0db0SJames Feist 
127073df0db0SJames Feist         if (profile)
127173df0db0SJames Feist         {
127273df0db0SJames Feist             if (std::find(supportedProfiles.begin(), supportedProfiles.end(),
127373df0db0SJames Feist                           *profile) == supportedProfiles.end())
127473df0db0SJames Feist             {
127573df0db0SJames Feist                 messages::actionParameterUnknown(response->res, "Profile",
127673df0db0SJames Feist                                                  *profile);
127773df0db0SJames Feist                 return;
127873df0db0SJames Feist             }
127973df0db0SJames Feist             currentProfile = *profile;
128073df0db0SJames Feist             crow::connections::systemBus->async_method_call(
128173df0db0SJames Feist                 [response](const boost::system::error_code ec) {
128273df0db0SJames Feist                     if (ec)
128373df0db0SJames Feist                     {
128473df0db0SJames Feist                         BMCWEB_LOG_ERROR << "Error patching profile" << ec;
128573df0db0SJames Feist                         messages::internalError(response->res);
128673df0db0SJames Feist                     }
128773df0db0SJames Feist                 },
128873df0db0SJames Feist                 profileConnection, profilePath,
128973df0db0SJames Feist                 "org.freedesktop.DBus.Properties", "Set", thermalModeIface,
129073df0db0SJames Feist                 "Current", std::variant<std::string>(*profile));
129173df0db0SJames Feist         }
129273df0db0SJames Feist 
129373df0db0SJames Feist         for (auto& containerPair : configuration)
129473df0db0SJames Feist         {
129573df0db0SJames Feist             auto& container = containerPair.second;
129673df0db0SJames Feist             if (!container)
129773df0db0SJames Feist             {
129873df0db0SJames Feist                 continue;
129973df0db0SJames Feist             }
130073df0db0SJames Feist             std::string& type = containerPair.first;
130173df0db0SJames Feist 
130273df0db0SJames Feist             for (nlohmann::json::iterator it = container->begin();
130373df0db0SJames Feist                  it != container->end(); it++)
130473df0db0SJames Feist             {
130573df0db0SJames Feist                 const auto& name = it.key();
130673df0db0SJames Feist                 auto pathItr =
130773df0db0SJames Feist                     std::find_if(managedObj.begin(), managedObj.end(),
130873df0db0SJames Feist                                  [&name](const auto& obj) {
130973df0db0SJames Feist                                      return boost::algorithm::ends_with(
131073df0db0SJames Feist                                          obj.first.str, "/" + name);
131173df0db0SJames Feist                                  });
131273df0db0SJames Feist                 boost::container::flat_map<std::string,
131373df0db0SJames Feist                                            dbus::utility::DbusVariantType>
131473df0db0SJames Feist                     output;
131573df0db0SJames Feist 
131673df0db0SJames Feist                 output.reserve(16); // The pid interface length
131773df0db0SJames Feist 
131873df0db0SJames Feist                 // determines if we're patching entity-manager or
131973df0db0SJames Feist                 // creating a new object
132073df0db0SJames Feist                 bool createNewObject = (pathItr == managedObj.end());
132173df0db0SJames Feist                 std::string iface;
132273df0db0SJames Feist                 if (type == "PidControllers" || type == "FanControllers")
132373df0db0SJames Feist                 {
132473df0db0SJames Feist                     iface = pidConfigurationIface;
132573df0db0SJames Feist                     if (!createNewObject &&
132673df0db0SJames Feist                         pathItr->second.find(pidConfigurationIface) ==
132773df0db0SJames Feist                             pathItr->second.end())
132873df0db0SJames Feist                     {
132973df0db0SJames Feist                         createNewObject = true;
133073df0db0SJames Feist                     }
133173df0db0SJames Feist                 }
133273df0db0SJames Feist                 else if (type == "FanZones")
133373df0db0SJames Feist                 {
133473df0db0SJames Feist                     iface = pidZoneConfigurationIface;
133573df0db0SJames Feist                     if (!createNewObject &&
133673df0db0SJames Feist                         pathItr->second.find(pidZoneConfigurationIface) ==
133773df0db0SJames Feist                             pathItr->second.end())
133873df0db0SJames Feist                     {
133973df0db0SJames Feist 
134073df0db0SJames Feist                         createNewObject = true;
134173df0db0SJames Feist                     }
134273df0db0SJames Feist                 }
134373df0db0SJames Feist                 else if (type == "StepwiseControllers")
134473df0db0SJames Feist                 {
134573df0db0SJames Feist                     iface = stepwiseConfigurationIface;
134673df0db0SJames Feist                     if (!createNewObject &&
134773df0db0SJames Feist                         pathItr->second.find(stepwiseConfigurationIface) ==
134873df0db0SJames Feist                             pathItr->second.end())
134973df0db0SJames Feist                     {
135073df0db0SJames Feist                         createNewObject = true;
135173df0db0SJames Feist                     }
135273df0db0SJames Feist                 }
135373df0db0SJames Feist                 BMCWEB_LOG_DEBUG << "Create new = " << createNewObject << "\n";
135473df0db0SJames Feist                 output["Name"] = boost::replace_all_copy(name, "_", " ");
135573df0db0SJames Feist 
135673df0db0SJames Feist                 std::string chassis;
135773df0db0SJames Feist                 CreatePIDRet ret = createPidInterface(
135873df0db0SJames Feist                     response, type, it, pathItr->first.str, managedObj,
135973df0db0SJames Feist                     createNewObject, output, chassis, currentProfile);
136073df0db0SJames Feist                 if (ret == CreatePIDRet::fail)
136173df0db0SJames Feist                 {
136273df0db0SJames Feist                     return;
136373df0db0SJames Feist                 }
136473df0db0SJames Feist                 else if (ret == CreatePIDRet::del)
136573df0db0SJames Feist                 {
136673df0db0SJames Feist                     continue;
136773df0db0SJames Feist                 }
136873df0db0SJames Feist 
136973df0db0SJames Feist                 if (!createNewObject)
137073df0db0SJames Feist                 {
137173df0db0SJames Feist                     for (const auto& property : output)
137273df0db0SJames Feist                     {
137373df0db0SJames Feist                         crow::connections::systemBus->async_method_call(
137473df0db0SJames Feist                             [response,
137573df0db0SJames Feist                              propertyName{std::string(property.first)}](
137673df0db0SJames Feist                                 const boost::system::error_code ec) {
137773df0db0SJames Feist                                 if (ec)
137873df0db0SJames Feist                                 {
137973df0db0SJames Feist                                     BMCWEB_LOG_ERROR << "Error patching "
138073df0db0SJames Feist                                                      << propertyName << ": "
138173df0db0SJames Feist                                                      << ec;
138273df0db0SJames Feist                                     messages::internalError(response->res);
138373df0db0SJames Feist                                     return;
138473df0db0SJames Feist                                 }
138573df0db0SJames Feist                                 messages::success(response->res);
138673df0db0SJames Feist                             },
138773df0db0SJames Feist                             "xyz.openbmc_project.EntityManager",
138873df0db0SJames Feist                             pathItr->first.str,
138973df0db0SJames Feist                             "org.freedesktop.DBus.Properties", "Set", iface,
139073df0db0SJames Feist                             property.first, property.second);
139173df0db0SJames Feist                     }
139273df0db0SJames Feist                 }
139373df0db0SJames Feist                 else
139473df0db0SJames Feist                 {
139573df0db0SJames Feist                     if (chassis.empty())
139673df0db0SJames Feist                     {
139773df0db0SJames Feist                         BMCWEB_LOG_ERROR << "Failed to get chassis from config";
139873df0db0SJames Feist                         messages::invalidObject(response->res, name);
139973df0db0SJames Feist                         return;
140073df0db0SJames Feist                     }
140173df0db0SJames Feist 
140273df0db0SJames Feist                     bool foundChassis = false;
140373df0db0SJames Feist                     for (const auto& obj : managedObj)
140473df0db0SJames Feist                     {
140573df0db0SJames Feist                         if (boost::algorithm::ends_with(obj.first.str, chassis))
140673df0db0SJames Feist                         {
140773df0db0SJames Feist                             chassis = obj.first.str;
140873df0db0SJames Feist                             foundChassis = true;
140973df0db0SJames Feist                             break;
141073df0db0SJames Feist                         }
141173df0db0SJames Feist                     }
141273df0db0SJames Feist                     if (!foundChassis)
141373df0db0SJames Feist                     {
141473df0db0SJames Feist                         BMCWEB_LOG_ERROR << "Failed to find chassis on dbus";
141573df0db0SJames Feist                         messages::resourceMissingAtURI(
141673df0db0SJames Feist                             response->res, "/redfish/v1/Chassis/" + chassis);
141773df0db0SJames Feist                         return;
141873df0db0SJames Feist                     }
141973df0db0SJames Feist 
142073df0db0SJames Feist                     crow::connections::systemBus->async_method_call(
142173df0db0SJames Feist                         [response](const boost::system::error_code ec) {
142273df0db0SJames Feist                             if (ec)
142373df0db0SJames Feist                             {
142473df0db0SJames Feist                                 BMCWEB_LOG_ERROR << "Error Adding Pid Object "
142573df0db0SJames Feist                                                  << ec;
142673df0db0SJames Feist                                 messages::internalError(response->res);
142773df0db0SJames Feist                                 return;
142873df0db0SJames Feist                             }
142973df0db0SJames Feist                             messages::success(response->res);
143073df0db0SJames Feist                         },
143173df0db0SJames Feist                         "xyz.openbmc_project.EntityManager", chassis,
143273df0db0SJames Feist                         "xyz.openbmc_project.AddObject", "AddObject", output);
143373df0db0SJames Feist                 }
143473df0db0SJames Feist             }
143573df0db0SJames Feist         }
143673df0db0SJames Feist     }
143773df0db0SJames Feist     std::shared_ptr<AsyncResp> asyncResp;
143873df0db0SJames Feist     std::vector<std::pair<std::string, std::optional<nlohmann::json>>>
143973df0db0SJames Feist         configuration;
144073df0db0SJames Feist     std::optional<std::string> profile;
144173df0db0SJames Feist     dbus::utility::ManagedObjectType managedObj;
144273df0db0SJames Feist     std::vector<std::string> supportedProfiles;
144373df0db0SJames Feist     std::string currentProfile;
144473df0db0SJames Feist     std::string profileConnection;
144573df0db0SJames Feist     std::string profilePath;
144673df0db0SJames Feist };
144773df0db0SJames Feist 
144873df0db0SJames Feist class Manager : public Node
144973df0db0SJames Feist {
145073df0db0SJames Feist   public:
145173df0db0SJames Feist     Manager(CrowApp& app) : Node(app, "/redfish/v1/Managers/bmc/")
145273df0db0SJames Feist     {
145373df0db0SJames Feist         uuid = app.template getMiddleware<crow::persistent_data::Middleware>()
145473df0db0SJames Feist                    .systemUuid;
145573df0db0SJames Feist         entityPrivileges = {
145673df0db0SJames Feist             {boost::beast::http::verb::get, {{"Login"}}},
145773df0db0SJames Feist             {boost::beast::http::verb::head, {{"Login"}}},
145873df0db0SJames Feist             {boost::beast::http::verb::patch, {{"ConfigureManager"}}},
145973df0db0SJames Feist             {boost::beast::http::verb::put, {{"ConfigureManager"}}},
146073df0db0SJames Feist             {boost::beast::http::verb::delete_, {{"ConfigureManager"}}},
146173df0db0SJames Feist             {boost::beast::http::verb::post, {{"ConfigureManager"}}}};
146273df0db0SJames Feist     }
146373df0db0SJames Feist 
146473df0db0SJames Feist   private:
146555c7b7a2SEd Tanous     void doGet(crow::Response& res, const crow::Request& req,
14661abe55efSEd Tanous                const std::vector<std::string>& params) override
14671abe55efSEd Tanous     {
14680f74e643SEd Tanous         res.jsonValue["@odata.id"] = "/redfish/v1/Managers/bmc";
14690f74e643SEd Tanous         res.jsonValue["@odata.type"] = "#Manager.v1_3_0.Manager";
14700f74e643SEd Tanous         res.jsonValue["@odata.context"] =
14710f74e643SEd Tanous             "/redfish/v1/$metadata#Manager.Manager";
14720f74e643SEd Tanous         res.jsonValue["Id"] = "bmc";
14730f74e643SEd Tanous         res.jsonValue["Name"] = "OpenBmc Manager";
14740f74e643SEd Tanous         res.jsonValue["Description"] = "Baseboard Management Controller";
14750f74e643SEd Tanous         res.jsonValue["PowerState"] = "On";
1476029573d4SEd Tanous         res.jsonValue["Status"] = {{"State", "Enabled"}, {"Health", "OK"}};
14770f74e643SEd Tanous         res.jsonValue["ManagerType"] = "BMC";
14783602e232SEd Tanous         res.jsonValue["UUID"] = systemd_utils::getUuid();
14793602e232SEd Tanous         res.jsonValue["ServiceEntryPointUUID"] = uuid;
14800f74e643SEd Tanous         res.jsonValue["Model"] = "OpenBmc"; // TODO(ed), get model
14810f74e643SEd Tanous 
14820f74e643SEd Tanous         res.jsonValue["LogServices"] = {
14830f74e643SEd Tanous             {"@odata.id", "/redfish/v1/Managers/bmc/LogServices"}};
14840f74e643SEd Tanous 
14850f74e643SEd Tanous         res.jsonValue["NetworkProtocol"] = {
14860f74e643SEd Tanous             {"@odata.id", "/redfish/v1/Managers/bmc/NetworkProtocol"}};
14870f74e643SEd Tanous 
14880f74e643SEd Tanous         res.jsonValue["EthernetInterfaces"] = {
14890f74e643SEd Tanous             {"@odata.id", "/redfish/v1/Managers/bmc/EthernetInterfaces"}};
14900f74e643SEd Tanous         // default oem data
14910f74e643SEd Tanous         nlohmann::json& oem = res.jsonValue["Oem"];
14920f74e643SEd Tanous         nlohmann::json& oemOpenbmc = oem["OpenBmc"];
14930f74e643SEd Tanous         oem["@odata.type"] = "#OemManager.Oem";
14940f74e643SEd Tanous         oem["@odata.id"] = "/redfish/v1/Managers/bmc#/Oem";
14950f74e643SEd Tanous         oem["@odata.context"] = "/redfish/v1/$metadata#OemManager.Oem";
14960f74e643SEd Tanous         oemOpenbmc["@odata.type"] = "#OemManager.OpenBmc";
14970f74e643SEd Tanous         oemOpenbmc["@odata.id"] = "/redfish/v1/Managers/bmc#/Oem/OpenBmc";
14980f74e643SEd Tanous         oemOpenbmc["@odata.context"] =
14990f74e643SEd Tanous             "/redfish/v1/$metadata#OemManager.OpenBmc";
15000f74e643SEd Tanous 
1501ed5befbdSJennifer Lee         // Update Actions object.
15020f74e643SEd Tanous         nlohmann::json& manager_reset =
15030f74e643SEd Tanous             res.jsonValue["Actions"]["#Manager.Reset"];
1504ed5befbdSJennifer Lee         manager_reset["target"] =
1505ed5befbdSJennifer Lee             "/redfish/v1/Managers/bmc/Actions/Manager.Reset";
1506ed5befbdSJennifer Lee         manager_reset["ResetType@Redfish.AllowableValues"] = {
1507ed5befbdSJennifer Lee             "GracefulRestart"};
1508ca537928SJennifer Lee 
1509cb92c03bSAndrew Geissler         res.jsonValue["DateTime"] = crow::utility::dateTimeNow();
1510474bfad5SSantosh Puranik 
1511474bfad5SSantosh Puranik         // Fill in GraphicalConsole and SerialConsole info
1512474bfad5SSantosh Puranik         res.jsonValue["SerialConsole"]["ServiceEnabled"] = true;
1513474bfad5SSantosh Puranik         res.jsonValue["SerialConsole"]["ConnectTypesSupported"] = {"IPMI",
1514474bfad5SSantosh Puranik                                                                    "SSH"};
1515ef47bb18SSantosh Puranik #ifdef BMCWEB_ENABLE_KVM
1516ef47bb18SSantosh Puranik         res.jsonValue["GraphicalConsole"]["ServiceEnabled"] = true;
1517ef47bb18SSantosh Puranik         res.jsonValue["GraphicalConsole"]["ConnectTypesSupported"] = {"KVMIP"};
1518ef47bb18SSantosh Puranik #endif // BMCWEB_ENABLE_KVM
1519474bfad5SSantosh Puranik 
1520603a6640SGunnar Mills         res.jsonValue["Links"]["ManagerForServers@odata.count"] = 1;
1521603a6640SGunnar Mills         res.jsonValue["Links"]["ManagerForServers"] = {
1522603a6640SGunnar Mills             {{"@odata.id", "/redfish/v1/Systems/system"}}};
152326f03899SShawn McCarney 
1524ed5befbdSJennifer Lee         std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
15255b4aa86bSJames Feist 
1526*b49ac873SJames Feist         auto health = std::make_shared<HealthPopulate>(asyncResp);
1527*b49ac873SJames Feist         health->isManagersHealth = true;
1528*b49ac873SJames Feist         health->populate();
1529*b49ac873SJames Feist 
1530ca537928SJennifer Lee         crow::connections::systemBus->async_method_call(
1531ca537928SJennifer Lee             [asyncResp](const boost::system::error_code ec,
15325b4aa86bSJames Feist                         const dbus::utility::ManagedObjectType& resp) {
1533ca537928SJennifer Lee                 if (ec)
1534ca537928SJennifer Lee                 {
1535ca537928SJennifer Lee                     BMCWEB_LOG_ERROR << "Error while getting Software Version";
1536f12894f8SJason M. Bills                     messages::internalError(asyncResp->res);
1537ca537928SJennifer Lee                     return;
1538ca537928SJennifer Lee                 }
1539ca537928SJennifer Lee 
1540ca537928SJennifer Lee                 for (auto& objpath : resp)
1541ca537928SJennifer Lee                 {
1542ca537928SJennifer Lee                     for (auto& interface : objpath.second)
1543ca537928SJennifer Lee                     {
15445f2caaefSJames Feist                         // If interface is
15455f2caaefSJames Feist                         // xyz.openbmc_project.Software.Version, this is
15465f2caaefSJames Feist                         // what we're looking for.
1547ca537928SJennifer Lee                         if (interface.first ==
1548ca537928SJennifer Lee                             "xyz.openbmc_project.Software.Version")
1549ca537928SJennifer Lee                         {
1550ca537928SJennifer Lee                             // Cut out everyting until last "/", ...
1551ca537928SJennifer Lee                             for (auto& property : interface.second)
1552ca537928SJennifer Lee                             {
1553ca537928SJennifer Lee                                 if (property.first == "Version")
1554ca537928SJennifer Lee                                 {
1555ca537928SJennifer Lee                                     const std::string* value =
1556abf2add6SEd Tanous                                         std::get_if<std::string>(
1557abf2add6SEd Tanous                                             &property.second);
1558ca537928SJennifer Lee                                     if (value == nullptr)
1559ca537928SJennifer Lee                                     {
1560ca537928SJennifer Lee                                         continue;
1561ca537928SJennifer Lee                                     }
1562ca537928SJennifer Lee                                     asyncResp->res
1563ca537928SJennifer Lee                                         .jsonValue["FirmwareVersion"] = *value;
1564ca537928SJennifer Lee                                 }
1565ca537928SJennifer Lee                             }
1566ca537928SJennifer Lee                         }
1567ca537928SJennifer Lee                     }
1568ca537928SJennifer Lee                 }
1569ca537928SJennifer Lee             },
1570ca537928SJennifer Lee             "xyz.openbmc_project.Software.BMC.Updater",
1571ca537928SJennifer Lee             "/xyz/openbmc_project/software",
1572ca537928SJennifer Lee             "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
15730f6b00bdSJames Feist 
157473df0db0SJames Feist         auto pids = std::make_shared<GetPIDValues>(asyncResp);
157573df0db0SJames Feist         pids->run();
1576c5d03ff4SJennifer Lee 
1577c5d03ff4SJennifer Lee         getMainChassisId(asyncResp, [](const std::string& chassisId,
1578c5d03ff4SJennifer Lee                                        const std::shared_ptr<AsyncResp> aRsp) {
1579c5d03ff4SJennifer Lee             aRsp->res.jsonValue["Links"]["ManagerForChassis@odata.count"] = 1;
1580c5d03ff4SJennifer Lee             aRsp->res.jsonValue["Links"]["ManagerForChassis"] = {
1581c5d03ff4SJennifer Lee                 {{"@odata.id", "/redfish/v1/Chassis/" + chassisId}}};
1582c5d03ff4SJennifer Lee         });
15830f6b00bdSJames Feist 
15840f6b00bdSJames Feist         static bool started = false;
15850f6b00bdSJames Feist 
15860f6b00bdSJames Feist         if (!started)
15870f6b00bdSJames Feist         {
15880f6b00bdSJames Feist             crow::connections::systemBus->async_method_call(
15890f6b00bdSJames Feist                 [asyncResp](const boost::system::error_code ec,
15900f6b00bdSJames Feist                             const std::variant<double>& resp) {
15910f6b00bdSJames Feist                     if (ec)
15920f6b00bdSJames Feist                     {
15930f6b00bdSJames Feist                         BMCWEB_LOG_ERROR << "Error while getting progress";
15940f6b00bdSJames Feist                         messages::internalError(asyncResp->res);
15950f6b00bdSJames Feist                         return;
15960f6b00bdSJames Feist                     }
15970f6b00bdSJames Feist                     const double* val = std::get_if<double>(&resp);
15980f6b00bdSJames Feist                     if (val == nullptr)
15990f6b00bdSJames Feist                     {
16000f6b00bdSJames Feist                         BMCWEB_LOG_ERROR
16010f6b00bdSJames Feist                             << "Invalid response while getting progress";
16020f6b00bdSJames Feist                         messages::internalError(asyncResp->res);
16030f6b00bdSJames Feist                         return;
16040f6b00bdSJames Feist                     }
16050f6b00bdSJames Feist                     if (*val < 1.0)
16060f6b00bdSJames Feist                     {
16070f6b00bdSJames Feist                         asyncResp->res.jsonValue["Status"]["State"] =
16080f6b00bdSJames Feist                             "Starting";
16090f6b00bdSJames Feist                         started = true;
16100f6b00bdSJames Feist                     }
16110f6b00bdSJames Feist                 },
16120f6b00bdSJames Feist                 "org.freedesktop.systemd1", "/org/freedesktop/systemd1",
16130f6b00bdSJames Feist                 "org.freedesktop.DBus.Properties", "Get",
16140f6b00bdSJames Feist                 "org.freedesktop.systemd1.Manager", "Progress");
16150f6b00bdSJames Feist         }
161683ff9ab6SJames Feist     }
16175b4aa86bSJames Feist 
16185b4aa86bSJames Feist     void doPatch(crow::Response& res, const crow::Request& req,
16195b4aa86bSJames Feist                  const std::vector<std::string>& params) override
16205b4aa86bSJames Feist     {
16210627a2c7SEd Tanous         std::optional<nlohmann::json> oem;
1622af5d6058SSantosh Puranik         std::optional<std::string> datetime;
16230627a2c7SEd Tanous 
1624af5d6058SSantosh Puranik         if (!json_util::readJson(req, res, "Oem", oem, "DateTime", datetime))
162583ff9ab6SJames Feist         {
162683ff9ab6SJames Feist             return;
162783ff9ab6SJames Feist         }
16280627a2c7SEd Tanous 
162983ff9ab6SJames Feist         std::shared_ptr<AsyncResp> response = std::make_shared<AsyncResp>(res);
16300627a2c7SEd Tanous 
16310627a2c7SEd Tanous         if (oem)
163283ff9ab6SJames Feist         {
16335f2caaefSJames Feist             std::optional<nlohmann::json> openbmc;
163443b761d0SEd Tanous             if (!redfish::json_util::readJson(*oem, res, "OpenBmc", openbmc))
163583ff9ab6SJames Feist             {
163643b761d0SEd Tanous                 BMCWEB_LOG_ERROR << "Line:" << __LINE__ << ", Illegal Property "
163743b761d0SEd Tanous                                  << oem->dump();
163883ff9ab6SJames Feist                 return;
163983ff9ab6SJames Feist             }
16405f2caaefSJames Feist             if (openbmc)
164183ff9ab6SJames Feist             {
16425f2caaefSJames Feist                 std::optional<nlohmann::json> fan;
164343b761d0SEd Tanous                 if (!redfish::json_util::readJson(*openbmc, res, "Fan", fan))
164483ff9ab6SJames Feist                 {
16455f2caaefSJames Feist                     BMCWEB_LOG_ERROR << "Line:" << __LINE__
16465f2caaefSJames Feist                                      << ", Illegal Property "
16475f2caaefSJames Feist                                      << openbmc->dump();
164883ff9ab6SJames Feist                     return;
164983ff9ab6SJames Feist                 }
16505f2caaefSJames Feist                 if (fan)
165183ff9ab6SJames Feist                 {
165273df0db0SJames Feist                     auto pid = std::make_shared<SetPIDValues>(response, *fan);
165373df0db0SJames Feist                     pid->run();
165483ff9ab6SJames Feist                 }
165583ff9ab6SJames Feist             }
165683ff9ab6SJames Feist         }
1657af5d6058SSantosh Puranik         if (datetime)
1658af5d6058SSantosh Puranik         {
1659af5d6058SSantosh Puranik             setDateTime(response, std::move(*datetime));
1660af5d6058SSantosh Puranik         }
1661af5d6058SSantosh Puranik     }
1662af5d6058SSantosh Puranik 
1663af5d6058SSantosh Puranik     void setDateTime(std::shared_ptr<AsyncResp> aResp,
1664af5d6058SSantosh Puranik                      std::string datetime) const
1665af5d6058SSantosh Puranik     {
1666af5d6058SSantosh Puranik         BMCWEB_LOG_DEBUG << "Set date time: " << datetime;
1667af5d6058SSantosh Puranik 
1668af5d6058SSantosh Puranik         std::stringstream stream(datetime);
1669af5d6058SSantosh Puranik         // Convert from ISO 8601 to boost local_time
1670af5d6058SSantosh Puranik         // (BMC only has time in UTC)
1671af5d6058SSantosh Puranik         boost::posix_time::ptime posixTime;
1672af5d6058SSantosh Puranik         boost::posix_time::ptime epoch(boost::gregorian::date(1970, 1, 1));
1673af5d6058SSantosh Puranik         // Facet gets deleted with the stringsteam
1674af5d6058SSantosh Puranik         auto ifc = std::make_unique<boost::local_time::local_time_input_facet>(
1675af5d6058SSantosh Puranik             "%Y-%m-%d %H:%M:%S%F %ZP");
1676af5d6058SSantosh Puranik         stream.imbue(std::locale(stream.getloc(), ifc.release()));
1677af5d6058SSantosh Puranik 
1678af5d6058SSantosh Puranik         boost::local_time::local_date_time ldt(
1679af5d6058SSantosh Puranik             boost::local_time::not_a_date_time);
1680af5d6058SSantosh Puranik 
1681af5d6058SSantosh Puranik         if (stream >> ldt)
1682af5d6058SSantosh Puranik         {
1683af5d6058SSantosh Puranik             posixTime = ldt.utc_time();
1684af5d6058SSantosh Puranik             boost::posix_time::time_duration dur = posixTime - epoch;
1685af5d6058SSantosh Puranik             uint64_t durMicroSecs =
1686af5d6058SSantosh Puranik                 static_cast<uint64_t>(dur.total_microseconds());
1687af5d6058SSantosh Puranik             crow::connections::systemBus->async_method_call(
1688af5d6058SSantosh Puranik                 [aResp{std::move(aResp)}, datetime{std::move(datetime)}](
1689af5d6058SSantosh Puranik                     const boost::system::error_code ec) {
1690af5d6058SSantosh Puranik                     if (ec)
1691af5d6058SSantosh Puranik                     {
1692af5d6058SSantosh Puranik                         BMCWEB_LOG_DEBUG << "Failed to set elapsed time. "
1693af5d6058SSantosh Puranik                                             "DBUS response error "
1694af5d6058SSantosh Puranik                                          << ec;
1695af5d6058SSantosh Puranik                         messages::internalError(aResp->res);
1696af5d6058SSantosh Puranik                         return;
1697af5d6058SSantosh Puranik                     }
1698af5d6058SSantosh Puranik                     aResp->res.jsonValue["DateTime"] = datetime;
1699af5d6058SSantosh Puranik                 },
1700af5d6058SSantosh Puranik                 "xyz.openbmc_project.Time.Manager",
1701af5d6058SSantosh Puranik                 "/xyz/openbmc_project/time/bmc",
1702af5d6058SSantosh Puranik                 "org.freedesktop.DBus.Properties", "Set",
1703af5d6058SSantosh Puranik                 "xyz.openbmc_project.Time.EpochTime", "Elapsed",
1704af5d6058SSantosh Puranik                 std::variant<uint64_t>(durMicroSecs));
1705af5d6058SSantosh Puranik         }
1706af5d6058SSantosh Puranik         else
1707af5d6058SSantosh Puranik         {
1708af5d6058SSantosh Puranik             messages::propertyValueFormatError(aResp->res, datetime,
1709af5d6058SSantosh Puranik                                                "DateTime");
1710af5d6058SSantosh Puranik             return;
1711af5d6058SSantosh Puranik         }
171283ff9ab6SJames Feist     }
17139c310685SBorawski.Lukasz 
17140f74e643SEd Tanous     std::string uuid;
17159c310685SBorawski.Lukasz };
17169c310685SBorawski.Lukasz 
17171abe55efSEd Tanous class ManagerCollection : public Node
17181abe55efSEd Tanous {
17199c310685SBorawski.Lukasz   public:
17201abe55efSEd Tanous     ManagerCollection(CrowApp& app) : Node(app, "/redfish/v1/Managers/")
17211abe55efSEd Tanous     {
1722a434f2bdSEd Tanous         entityPrivileges = {
1723a434f2bdSEd Tanous             {boost::beast::http::verb::get, {{"Login"}}},
1724e0d918bcSEd Tanous             {boost::beast::http::verb::head, {{"Login"}}},
1725e0d918bcSEd Tanous             {boost::beast::http::verb::patch, {{"ConfigureManager"}}},
1726e0d918bcSEd Tanous             {boost::beast::http::verb::put, {{"ConfigureManager"}}},
1727e0d918bcSEd Tanous             {boost::beast::http::verb::delete_, {{"ConfigureManager"}}},
1728e0d918bcSEd Tanous             {boost::beast::http::verb::post, {{"ConfigureManager"}}}};
17299c310685SBorawski.Lukasz     }
17309c310685SBorawski.Lukasz 
17319c310685SBorawski.Lukasz   private:
173255c7b7a2SEd Tanous     void doGet(crow::Response& res, const crow::Request& req,
17331abe55efSEd Tanous                const std::vector<std::string>& params) override
17341abe55efSEd Tanous     {
173583ff9ab6SJames Feist         // Collections don't include the static data added by SubRoute
173683ff9ab6SJames Feist         // because it has a duplicate entry for members
173755c7b7a2SEd Tanous         res.jsonValue["@odata.id"] = "/redfish/v1/Managers";
173855c7b7a2SEd Tanous         res.jsonValue["@odata.type"] = "#ManagerCollection.ManagerCollection";
173955c7b7a2SEd Tanous         res.jsonValue["@odata.context"] =
174055c7b7a2SEd Tanous             "/redfish/v1/$metadata#ManagerCollection.ManagerCollection";
174155c7b7a2SEd Tanous         res.jsonValue["Name"] = "Manager Collection";
174255c7b7a2SEd Tanous         res.jsonValue["Members@odata.count"] = 1;
174355c7b7a2SEd Tanous         res.jsonValue["Members"] = {
17445b4aa86bSJames Feist             {{"@odata.id", "/redfish/v1/Managers/bmc"}}};
17459c310685SBorawski.Lukasz         res.end();
17469c310685SBorawski.Lukasz     }
17479c310685SBorawski.Lukasz };
17489c310685SBorawski.Lukasz } // namespace redfish
1749