xref: /openbmc/bmcweb/features/redfish/lib/managers.hpp (revision c33a90ec049bc82054106bd4e09381797df72002)
19c310685SBorawski.Lukasz /*
29c310685SBorawski.Lukasz // Copyright (c) 2018 Intel Corporation
39c310685SBorawski.Lukasz //
49c310685SBorawski.Lukasz // Licensed under the Apache License, Version 2.0 (the "License");
59c310685SBorawski.Lukasz // you may not use this file except in compliance with the License.
69c310685SBorawski.Lukasz // You may obtain a copy of the License at
79c310685SBorawski.Lukasz //
89c310685SBorawski.Lukasz //      http://www.apache.org/licenses/LICENSE-2.0
99c310685SBorawski.Lukasz //
109c310685SBorawski.Lukasz // Unless required by applicable law or agreed to in writing, software
119c310685SBorawski.Lukasz // distributed under the License is distributed on an "AS IS" BASIS,
129c310685SBorawski.Lukasz // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
139c310685SBorawski.Lukasz // See the License for the specific language governing permissions and
149c310685SBorawski.Lukasz // limitations under the License.
159c310685SBorawski.Lukasz */
169c310685SBorawski.Lukasz #pragma once
179c310685SBorawski.Lukasz 
189c310685SBorawski.Lukasz #include "node.hpp"
199c310685SBorawski.Lukasz 
205b4aa86bSJames Feist #include <boost/algorithm/string/replace.hpp>
215b4aa86bSJames Feist #include <dbus_utility.hpp>
22abf2add6SEd Tanous #include <variant>
235b4aa86bSJames Feist 
241abe55efSEd Tanous namespace redfish
251abe55efSEd Tanous {
26ed5befbdSJennifer Lee 
27ed5befbdSJennifer Lee /**
28ed5befbdSJennifer Lee  * ManagerActionsReset class supports handle POST method for Reset action.
29ed5befbdSJennifer Lee  * The class retrieves and sends data directly to dbus.
30ed5befbdSJennifer Lee  */
31ed5befbdSJennifer Lee class ManagerActionsReset : public Node
32ed5befbdSJennifer Lee {
33ed5befbdSJennifer Lee   public:
34ed5befbdSJennifer Lee     ManagerActionsReset(CrowApp& app) :
35ed5befbdSJennifer Lee         Node(app, "/redfish/v1/Managers/bmc/Actions/Manager.Reset/")
36ed5befbdSJennifer Lee     {
37ed5befbdSJennifer Lee         entityPrivileges = {
38ed5befbdSJennifer Lee             {boost::beast::http::verb::get, {{"Login"}}},
39ed5befbdSJennifer Lee             {boost::beast::http::verb::head, {{"Login"}}},
40ed5befbdSJennifer Lee             {boost::beast::http::verb::patch, {{"ConfigureManager"}}},
41ed5befbdSJennifer Lee             {boost::beast::http::verb::put, {{"ConfigureManager"}}},
42ed5befbdSJennifer Lee             {boost::beast::http::verb::delete_, {{"ConfigureManager"}}},
43ed5befbdSJennifer Lee             {boost::beast::http::verb::post, {{"ConfigureManager"}}}};
44ed5befbdSJennifer Lee     }
45ed5befbdSJennifer Lee 
46ed5befbdSJennifer Lee   private:
47ed5befbdSJennifer Lee     /**
48ed5befbdSJennifer Lee      * Function handles POST method request.
49ed5befbdSJennifer Lee      * Analyzes POST body message before sends Reset request data to dbus.
50ed5befbdSJennifer Lee      * OpenBMC allows for ResetType is GracefulRestart only.
51ed5befbdSJennifer Lee      */
52ed5befbdSJennifer Lee     void doPost(crow::Response& res, const crow::Request& req,
53ed5befbdSJennifer Lee                 const std::vector<std::string>& params) override
54ed5befbdSJennifer Lee     {
55ed5befbdSJennifer Lee         std::string resetType;
56ed5befbdSJennifer Lee 
57ed5befbdSJennifer Lee         if (!json_util::readJson(req, res, "ResetType", resetType))
58ed5befbdSJennifer Lee         {
59ed5befbdSJennifer Lee             return;
60ed5befbdSJennifer Lee         }
61ed5befbdSJennifer Lee 
62ed5befbdSJennifer Lee         if (resetType != "GracefulRestart")
63ed5befbdSJennifer Lee         {
64ed5befbdSJennifer Lee             res.result(boost::beast::http::status::bad_request);
65ed5befbdSJennifer Lee             messages::actionParameterNotSupported(res, resetType, "ResetType");
66ed5befbdSJennifer Lee             BMCWEB_LOG_ERROR << "Request incorrect action parameter: "
67ed5befbdSJennifer Lee                              << resetType;
68ed5befbdSJennifer Lee             res.end();
69ed5befbdSJennifer Lee             return;
70ed5befbdSJennifer Lee         }
71ed5befbdSJennifer Lee         doBMCGracefulRestart(res, req, params);
72ed5befbdSJennifer Lee     }
73ed5befbdSJennifer Lee 
74ed5befbdSJennifer Lee     /**
75ed5befbdSJennifer Lee      * Function transceives data with dbus directly.
76ed5befbdSJennifer Lee      * All BMC state properties will be retrieved before sending reset request.
77ed5befbdSJennifer Lee      */
78ed5befbdSJennifer Lee     void doBMCGracefulRestart(crow::Response& res, const crow::Request& req,
79ed5befbdSJennifer Lee                               const std::vector<std::string>& params)
80ed5befbdSJennifer Lee     {
81ed5befbdSJennifer Lee         const char* processName = "xyz.openbmc_project.State.BMC";
82ed5befbdSJennifer Lee         const char* objectPath = "/xyz/openbmc_project/state/bmc0";
83ed5befbdSJennifer Lee         const char* interfaceName = "xyz.openbmc_project.State.BMC";
84ed5befbdSJennifer Lee         const std::string& propertyValue =
85ed5befbdSJennifer Lee             "xyz.openbmc_project.State.BMC.Transition.Reboot";
86ed5befbdSJennifer Lee         const char* destProperty = "RequestedBMCTransition";
87ed5befbdSJennifer Lee 
88ed5befbdSJennifer Lee         // Create the D-Bus variant for D-Bus call.
89ed5befbdSJennifer Lee         VariantType dbusPropertyValue(propertyValue);
90ed5befbdSJennifer Lee 
91ed5befbdSJennifer Lee         std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
92ed5befbdSJennifer Lee 
93ed5befbdSJennifer Lee         crow::connections::systemBus->async_method_call(
94ed5befbdSJennifer Lee             [asyncResp](const boost::system::error_code ec) {
95ed5befbdSJennifer Lee                 // Use "Set" method to set the property value.
96ed5befbdSJennifer Lee                 if (ec)
97ed5befbdSJennifer Lee                 {
98ed5befbdSJennifer Lee                     BMCWEB_LOG_ERROR << "[Set] Bad D-Bus request error: " << ec;
99ed5befbdSJennifer Lee                     messages::internalError(asyncResp->res);
100ed5befbdSJennifer Lee                     return;
101ed5befbdSJennifer Lee                 }
102ed5befbdSJennifer Lee 
103ed5befbdSJennifer Lee                 messages::success(asyncResp->res);
104ed5befbdSJennifer Lee             },
105ed5befbdSJennifer Lee             processName, objectPath, "org.freedesktop.DBus.Properties", "Set",
106ed5befbdSJennifer Lee             interfaceName, destProperty, dbusPropertyValue);
107ed5befbdSJennifer Lee     }
108ed5befbdSJennifer Lee };
109ed5befbdSJennifer Lee 
1105b4aa86bSJames Feist static constexpr const char* objectManagerIface =
1115b4aa86bSJames Feist     "org.freedesktop.DBus.ObjectManager";
1125b4aa86bSJames Feist static constexpr const char* pidConfigurationIface =
1135b4aa86bSJames Feist     "xyz.openbmc_project.Configuration.Pid";
1145b4aa86bSJames Feist static constexpr const char* pidZoneConfigurationIface =
1155b4aa86bSJames Feist     "xyz.openbmc_project.Configuration.Pid.Zone";
116b7a08d04SJames Feist static constexpr const char* stepwiseConfigurationIface =
117b7a08d04SJames Feist     "xyz.openbmc_project.Configuration.Stepwise";
1189c310685SBorawski.Lukasz 
1195b4aa86bSJames Feist static void asyncPopulatePid(const std::string& connection,
1205b4aa86bSJames Feist                              const std::string& path,
1215b4aa86bSJames Feist                              std::shared_ptr<AsyncResp> asyncResp)
1225b4aa86bSJames Feist {
1235b4aa86bSJames Feist 
1245b4aa86bSJames Feist     crow::connections::systemBus->async_method_call(
1255b4aa86bSJames Feist         [asyncResp](const boost::system::error_code ec,
1265b4aa86bSJames Feist                     const dbus::utility::ManagedObjectType& managedObj) {
1275b4aa86bSJames Feist             if (ec)
1285b4aa86bSJames Feist             {
1295b4aa86bSJames Feist                 BMCWEB_LOG_ERROR << ec;
1305b4aa86bSJames Feist                 asyncResp->res.jsonValue.clear();
131f12894f8SJason M. Bills                 messages::internalError(asyncResp->res);
1325b4aa86bSJames Feist                 return;
1335b4aa86bSJames Feist             }
1345b4aa86bSJames Feist             nlohmann::json& configRoot =
1355b4aa86bSJames Feist                 asyncResp->res.jsonValue["Oem"]["OpenBmc"]["Fan"];
1365b4aa86bSJames Feist             nlohmann::json& fans = configRoot["FanControllers"];
1375b4aa86bSJames Feist             fans["@odata.type"] = "#OemManager.FanControllers";
1385b4aa86bSJames Feist             fans["@odata.context"] =
1395b4aa86bSJames Feist                 "/redfish/v1/$metadata#OemManager.FanControllers";
1405b4aa86bSJames Feist             fans["@odata.id"] = "/redfish/v1/Managers/bmc#/Oem/OpenBmc/"
1415b4aa86bSJames Feist                                 "Fan/FanControllers";
1425b4aa86bSJames Feist 
1435b4aa86bSJames Feist             nlohmann::json& pids = configRoot["PidControllers"];
1445b4aa86bSJames Feist             pids["@odata.type"] = "#OemManager.PidControllers";
1455b4aa86bSJames Feist             pids["@odata.context"] =
1465b4aa86bSJames Feist                 "/redfish/v1/$metadata#OemManager.PidControllers";
1475b4aa86bSJames Feist             pids["@odata.id"] =
1485b4aa86bSJames Feist                 "/redfish/v1/Managers/bmc#/Oem/OpenBmc/Fan/PidControllers";
1495b4aa86bSJames Feist 
150b7a08d04SJames Feist             nlohmann::json& stepwise = configRoot["StepwiseControllers"];
151b7a08d04SJames Feist             stepwise["@odata.type"] = "#OemManager.StepwiseControllers";
152b7a08d04SJames Feist             stepwise["@odata.context"] =
153b7a08d04SJames Feist                 "/redfish/v1/$metadata#OemManager.StepwiseControllers";
154b7a08d04SJames Feist             stepwise["@odata.id"] =
155b7a08d04SJames Feist                 "/redfish/v1/Managers/bmc#/Oem/OpenBmc/Fan/StepwiseControllers";
156b7a08d04SJames Feist 
1575b4aa86bSJames Feist             nlohmann::json& zones = configRoot["FanZones"];
1585b4aa86bSJames Feist             zones["@odata.id"] =
1595b4aa86bSJames Feist                 "/redfish/v1/Managers/bmc#/Oem/OpenBmc/Fan/FanZones";
1605b4aa86bSJames Feist             zones["@odata.type"] = "#OemManager.FanZones";
1615b4aa86bSJames Feist             zones["@odata.context"] =
1625b4aa86bSJames Feist                 "/redfish/v1/$metadata#OemManager.FanZones";
1635b4aa86bSJames Feist             configRoot["@odata.id"] =
1645b4aa86bSJames Feist                 "/redfish/v1/Managers/bmc#/Oem/OpenBmc/Fan";
1655b4aa86bSJames Feist             configRoot["@odata.type"] = "#OemManager.Fan";
1665b4aa86bSJames Feist             configRoot["@odata.context"] =
1675b4aa86bSJames Feist                 "/redfish/v1/$metadata#OemManager.Fan";
1685b4aa86bSJames Feist 
1695b4aa86bSJames Feist             for (const auto& pathPair : managedObj)
1705b4aa86bSJames Feist             {
1715b4aa86bSJames Feist                 for (const auto& intfPair : pathPair.second)
1725b4aa86bSJames Feist                 {
1735b4aa86bSJames Feist                     if (intfPair.first != pidConfigurationIface &&
174b7a08d04SJames Feist                         intfPair.first != pidZoneConfigurationIface &&
175b7a08d04SJames Feist                         intfPair.first != stepwiseConfigurationIface)
1765b4aa86bSJames Feist                     {
1775b4aa86bSJames Feist                         continue;
1785b4aa86bSJames Feist                     }
1795b4aa86bSJames Feist                     auto findName = intfPair.second.find("Name");
1805b4aa86bSJames Feist                     if (findName == intfPair.second.end())
1815b4aa86bSJames Feist                     {
1825b4aa86bSJames Feist                         BMCWEB_LOG_ERROR << "Pid Field missing Name";
183a08b46ccSJason M. Bills                         messages::internalError(asyncResp->res);
1845b4aa86bSJames Feist                         return;
1855b4aa86bSJames Feist                     }
1865b4aa86bSJames Feist                     const std::string* namePtr =
187abf2add6SEd Tanous                         std::get_if<std::string>(&findName->second);
1885b4aa86bSJames Feist                     if (namePtr == nullptr)
1895b4aa86bSJames Feist                     {
1905b4aa86bSJames Feist                         BMCWEB_LOG_ERROR << "Pid Name Field illegal";
191b7a08d04SJames Feist                         messages::internalError(asyncResp->res);
1925b4aa86bSJames Feist                         return;
1935b4aa86bSJames Feist                     }
1945b4aa86bSJames Feist 
1955b4aa86bSJames Feist                     std::string name = *namePtr;
1965b4aa86bSJames Feist                     dbus::utility::escapePathForDbus(name);
197b7a08d04SJames Feist                     nlohmann::json* config = nullptr;
198*c33a90ecSJames Feist 
199*c33a90ecSJames Feist                     const std::string* classPtr = nullptr;
200*c33a90ecSJames Feist                     auto findClass = intfPair.second.find("Class");
201*c33a90ecSJames Feist                     if (findClass != intfPair.second.end())
202*c33a90ecSJames Feist                     {
203*c33a90ecSJames Feist                         classPtr = std::get_if<std::string>(&findClass->second);
204*c33a90ecSJames Feist                     }
205*c33a90ecSJames Feist 
2065b4aa86bSJames Feist                     if (intfPair.first == pidZoneConfigurationIface)
2075b4aa86bSJames Feist                     {
2085b4aa86bSJames Feist                         std::string chassis;
2095b4aa86bSJames Feist                         if (!dbus::utility::getNthStringFromPath(
2105b4aa86bSJames Feist                                 pathPair.first.str, 5, chassis))
2115b4aa86bSJames Feist                         {
2125b4aa86bSJames Feist                             chassis = "#IllegalValue";
2135b4aa86bSJames Feist                         }
2145b4aa86bSJames Feist                         nlohmann::json& zone = zones[name];
2155b4aa86bSJames Feist                         zone["Chassis"] = {
2165b4aa86bSJames Feist                             {"@odata.id", "/redfish/v1/Chassis/" + chassis}};
2175b4aa86bSJames Feist                         zone["@odata.id"] = "/redfish/v1/Managers/bmc#/Oem/"
2185b4aa86bSJames Feist                                             "OpenBmc/Fan/FanZones/" +
2195b4aa86bSJames Feist                                             name;
2205b4aa86bSJames Feist                         zone["@odata.type"] = "#OemManager.FanZone";
2215b4aa86bSJames Feist                         zone["@odata.context"] =
2225b4aa86bSJames Feist                             "/redfish/v1/$metadata#OemManager.FanZone";
223b7a08d04SJames Feist                         config = &zone;
2245b4aa86bSJames Feist                     }
2255b4aa86bSJames Feist 
226b7a08d04SJames Feist                     else if (intfPair.first == stepwiseConfigurationIface)
2275b4aa86bSJames Feist                     {
228*c33a90ecSJames Feist                         if (classPtr == nullptr)
229*c33a90ecSJames Feist                         {
230*c33a90ecSJames Feist                             BMCWEB_LOG_ERROR << "Pid Class Field illegal";
231*c33a90ecSJames Feist                             messages::internalError(asyncResp->res);
232*c33a90ecSJames Feist                             return;
233*c33a90ecSJames Feist                         }
234*c33a90ecSJames Feist 
235b7a08d04SJames Feist                         nlohmann::json& controller = stepwise[name];
236b7a08d04SJames Feist                         config = &controller;
2375b4aa86bSJames Feist 
238b7a08d04SJames Feist                         controller["@odata.id"] =
239b7a08d04SJames Feist                             "/redfish/v1/Managers/bmc#/Oem/"
240b7a08d04SJames Feist                             "OpenBmc/Fan/StepwiseControllers/" +
241b7a08d04SJames Feist                             std::string(name);
242b7a08d04SJames Feist                         controller["@odata.type"] =
243b7a08d04SJames Feist                             "#OemManager.StepwiseController";
244b7a08d04SJames Feist 
245b7a08d04SJames Feist                         controller["@odata.context"] =
246b7a08d04SJames Feist                             "/redfish/v1/"
247b7a08d04SJames Feist                             "$metadata#OemManager.StepwiseController";
248*c33a90ecSJames Feist                         controller["Direction"] = *classPtr;
2495b4aa86bSJames Feist                     }
2505b4aa86bSJames Feist 
2515b4aa86bSJames Feist                     // pid and fans are off the same configuration
252b7a08d04SJames Feist                     else if (intfPair.first == pidConfigurationIface)
2535b4aa86bSJames Feist                     {
254*c33a90ecSJames Feist 
2555b4aa86bSJames Feist                         if (classPtr == nullptr)
2565b4aa86bSJames Feist                         {
2575b4aa86bSJames Feist                             BMCWEB_LOG_ERROR << "Pid Class Field illegal";
258a08b46ccSJason M. Bills                             messages::internalError(asyncResp->res);
2595b4aa86bSJames Feist                             return;
2605b4aa86bSJames Feist                         }
2615b4aa86bSJames Feist                         bool isFan = *classPtr == "fan";
2625b4aa86bSJames Feist                         nlohmann::json& element =
2635b4aa86bSJames Feist                             isFan ? fans[name] : pids[name];
264b7a08d04SJames Feist                         config = &element;
2655b4aa86bSJames Feist                         if (isFan)
2665b4aa86bSJames Feist                         {
2675b4aa86bSJames Feist                             element["@odata.id"] =
2685b4aa86bSJames Feist                                 "/redfish/v1/Managers/bmc#/Oem/"
2695b4aa86bSJames Feist                                 "OpenBmc/Fan/FanControllers/" +
2705b4aa86bSJames Feist                                 std::string(name);
2715b4aa86bSJames Feist                             element["@odata.type"] =
2725b4aa86bSJames Feist                                 "#OemManager.FanController";
2735b4aa86bSJames Feist 
2745b4aa86bSJames Feist                             element["@odata.context"] =
2755b4aa86bSJames Feist                                 "/redfish/v1/"
2765b4aa86bSJames Feist                                 "$metadata#OemManager.FanController";
2775b4aa86bSJames Feist                         }
2785b4aa86bSJames Feist                         else
2795b4aa86bSJames Feist                         {
2805b4aa86bSJames Feist                             element["@odata.id"] =
2815b4aa86bSJames Feist                                 "/redfish/v1/Managers/bmc#/Oem/"
2825b4aa86bSJames Feist                                 "OpenBmc/Fan/PidControllers/" +
2835b4aa86bSJames Feist                                 std::string(name);
2845b4aa86bSJames Feist                             element["@odata.type"] =
2855b4aa86bSJames Feist                                 "#OemManager.PidController";
2865b4aa86bSJames Feist                             element["@odata.context"] =
2875b4aa86bSJames Feist                                 "/redfish/v1/$metadata"
2885b4aa86bSJames Feist                                 "#OemManager.PidController";
2895b4aa86bSJames Feist                         }
290b7a08d04SJames Feist                     }
291b7a08d04SJames Feist                     else
292b7a08d04SJames Feist                     {
293b7a08d04SJames Feist                         BMCWEB_LOG_ERROR << "Unexpected configuration";
294b7a08d04SJames Feist                         messages::internalError(asyncResp->res);
295b7a08d04SJames Feist                         return;
296b7a08d04SJames Feist                     }
297b7a08d04SJames Feist 
298b7a08d04SJames Feist                     // used for making maps out of 2 vectors
299b7a08d04SJames Feist                     const std::vector<double>* keys = nullptr;
300b7a08d04SJames Feist                     const std::vector<double>* values = nullptr;
301b7a08d04SJames Feist 
302b7a08d04SJames Feist                     for (const auto& propertyPair : intfPair.second)
303b7a08d04SJames Feist                     {
304b7a08d04SJames Feist                         if (propertyPair.first == "Type" ||
305b7a08d04SJames Feist                             propertyPair.first == "Class" ||
306b7a08d04SJames Feist                             propertyPair.first == "Name")
307b7a08d04SJames Feist                         {
308b7a08d04SJames Feist                             continue;
309b7a08d04SJames Feist                         }
310b7a08d04SJames Feist 
311b7a08d04SJames Feist                         // zones
312b7a08d04SJames Feist                         if (intfPair.first == pidZoneConfigurationIface)
313b7a08d04SJames Feist                         {
314b7a08d04SJames Feist                             const double* ptr =
315abf2add6SEd Tanous                                 std::get_if<double>(&propertyPair.second);
316b7a08d04SJames Feist                             if (ptr == nullptr)
317b7a08d04SJames Feist                             {
318b7a08d04SJames Feist                                 BMCWEB_LOG_ERROR << "Field Illegal "
319b7a08d04SJames Feist                                                  << propertyPair.first;
320b7a08d04SJames Feist                                 messages::internalError(asyncResp->res);
321b7a08d04SJames Feist                                 return;
322b7a08d04SJames Feist                             }
323b7a08d04SJames Feist                             (*config)[propertyPair.first] = *ptr;
324b7a08d04SJames Feist                         }
325b7a08d04SJames Feist 
326b7a08d04SJames Feist                         if (intfPair.first == stepwiseConfigurationIface)
327b7a08d04SJames Feist                         {
328b7a08d04SJames Feist                             if (propertyPair.first == "Reading" ||
329b7a08d04SJames Feist                                 propertyPair.first == "Output")
330b7a08d04SJames Feist                             {
331b7a08d04SJames Feist                                 const std::vector<double>* ptr =
332abf2add6SEd Tanous                                     std::get_if<std::vector<double>>(
333b7a08d04SJames Feist                                         &propertyPair.second);
334b7a08d04SJames Feist 
335b7a08d04SJames Feist                                 if (ptr == nullptr)
336b7a08d04SJames Feist                                 {
337b7a08d04SJames Feist                                     BMCWEB_LOG_ERROR << "Field Illegal "
338b7a08d04SJames Feist                                                      << propertyPair.first;
339b7a08d04SJames Feist                                     messages::internalError(asyncResp->res);
340b7a08d04SJames Feist                                     return;
341b7a08d04SJames Feist                                 }
342b7a08d04SJames Feist 
343b7a08d04SJames Feist                                 if (propertyPair.first == "Reading")
344b7a08d04SJames Feist                                 {
345b7a08d04SJames Feist                                     keys = ptr;
346b7a08d04SJames Feist                                 }
347b7a08d04SJames Feist                                 else
348b7a08d04SJames Feist                                 {
349b7a08d04SJames Feist                                     values = ptr;
350b7a08d04SJames Feist                                 }
351b7a08d04SJames Feist                                 if (keys && values)
352b7a08d04SJames Feist                                 {
353b7a08d04SJames Feist                                     if (keys->size() != values->size())
354b7a08d04SJames Feist                                     {
355b7a08d04SJames Feist                                         BMCWEB_LOG_ERROR
356b7a08d04SJames Feist                                             << "Reading and Output size don't "
357b7a08d04SJames Feist                                                "match ";
358b7a08d04SJames Feist                                         messages::internalError(asyncResp->res);
359b7a08d04SJames Feist                                         return;
360b7a08d04SJames Feist                                     }
361b7a08d04SJames Feist                                     nlohmann::json& steps = (*config)["Steps"];
362b7a08d04SJames Feist                                     steps = nlohmann::json::array();
363b7a08d04SJames Feist                                     for (size_t ii = 0; ii < keys->size(); ii++)
364b7a08d04SJames Feist                                     {
365b7a08d04SJames Feist                                         steps.push_back(
366b7a08d04SJames Feist                                             {{"Target", (*keys)[ii]},
367b7a08d04SJames Feist                                              {"Output", (*values)[ii]}});
368b7a08d04SJames Feist                                     }
369b7a08d04SJames Feist                                 }
370b7a08d04SJames Feist                             }
371b7a08d04SJames Feist                             if (propertyPair.first == "NegativeHysteresis" ||
372b7a08d04SJames Feist                                 propertyPair.first == "PositiveHysteresis")
373b7a08d04SJames Feist                             {
374b7a08d04SJames Feist                                 const double* ptr =
375abf2add6SEd Tanous                                     std::get_if<double>(&propertyPair.second);
376b7a08d04SJames Feist                                 if (ptr == nullptr)
377b7a08d04SJames Feist                                 {
378b7a08d04SJames Feist                                     BMCWEB_LOG_ERROR << "Field Illegal "
379b7a08d04SJames Feist                                                      << propertyPair.first;
380b7a08d04SJames Feist                                     messages::internalError(asyncResp->res);
381b7a08d04SJames Feist                                     return;
382b7a08d04SJames Feist                                 }
383b7a08d04SJames Feist                                 (*config)[propertyPair.first] = *ptr;
384b7a08d04SJames Feist                             }
385b7a08d04SJames Feist                         }
386b7a08d04SJames Feist 
387b7a08d04SJames Feist                         // pid and fans are off the same configuration
388b7a08d04SJames Feist                         if (intfPair.first == pidConfigurationIface ||
389b7a08d04SJames Feist                             intfPair.first == stepwiseConfigurationIface)
390b7a08d04SJames Feist                         {
3915b4aa86bSJames Feist 
3925b4aa86bSJames Feist                             if (propertyPair.first == "Zones")
3935b4aa86bSJames Feist                             {
3945b4aa86bSJames Feist                                 const std::vector<std::string>* inputs =
395abf2add6SEd Tanous                                     std::get_if<std::vector<std::string>>(
3961b6b96c5SEd Tanous                                         &propertyPair.second);
3975b4aa86bSJames Feist 
3985b4aa86bSJames Feist                                 if (inputs == nullptr)
3995b4aa86bSJames Feist                                 {
4005b4aa86bSJames Feist                                     BMCWEB_LOG_ERROR
4015b4aa86bSJames Feist                                         << "Zones Pid Field Illegal";
402a08b46ccSJason M. Bills                                     messages::internalError(asyncResp->res);
4035b4aa86bSJames Feist                                     return;
4045b4aa86bSJames Feist                                 }
405b7a08d04SJames Feist                                 auto& data = (*config)[propertyPair.first];
4065b4aa86bSJames Feist                                 data = nlohmann::json::array();
4075b4aa86bSJames Feist                                 for (std::string itemCopy : *inputs)
4085b4aa86bSJames Feist                                 {
4095b4aa86bSJames Feist                                     dbus::utility::escapePathForDbus(itemCopy);
4105b4aa86bSJames Feist                                     data.push_back(
4115b4aa86bSJames Feist                                         {{"@odata.id",
4125b4aa86bSJames Feist                                           "/redfish/v1/Managers/bmc#/Oem/"
4135b4aa86bSJames Feist                                           "OpenBmc/Fan/FanZones/" +
4145b4aa86bSJames Feist                                               itemCopy}});
4155b4aa86bSJames Feist                                 }
4165b4aa86bSJames Feist                             }
4175b4aa86bSJames Feist                             // todo(james): may never happen, but this
4185b4aa86bSJames Feist                             // assumes configuration data referenced in the
4195b4aa86bSJames Feist                             // PID config is provided by the same daemon, we
4205b4aa86bSJames Feist                             // could add another loop to cover all cases,
4215b4aa86bSJames Feist                             // but I'm okay kicking this can down the road a
4225b4aa86bSJames Feist                             // bit
4235b4aa86bSJames Feist 
4245b4aa86bSJames Feist                             else if (propertyPair.first == "Inputs" ||
4255b4aa86bSJames Feist                                      propertyPair.first == "Outputs")
4265b4aa86bSJames Feist                             {
427b7a08d04SJames Feist                                 auto& data = (*config)[propertyPair.first];
4285b4aa86bSJames Feist                                 const std::vector<std::string>* inputs =
429abf2add6SEd Tanous                                     std::get_if<std::vector<std::string>>(
4301b6b96c5SEd Tanous                                         &propertyPair.second);
4315b4aa86bSJames Feist 
4325b4aa86bSJames Feist                                 if (inputs == nullptr)
4335b4aa86bSJames Feist                                 {
4345b4aa86bSJames Feist                                     BMCWEB_LOG_ERROR << "Field Illegal "
4355b4aa86bSJames Feist                                                      << propertyPair.first;
436f12894f8SJason M. Bills                                     messages::internalError(asyncResp->res);
4375b4aa86bSJames Feist                                     return;
4385b4aa86bSJames Feist                                 }
4395b4aa86bSJames Feist                                 data = *inputs;
4405b4aa86bSJames Feist                             } // doubles
4415b4aa86bSJames Feist                             else if (propertyPair.first ==
4425b4aa86bSJames Feist                                          "FFGainCoefficient" ||
4435b4aa86bSJames Feist                                      propertyPair.first == "FFOffCoefficient" ||
4445b4aa86bSJames Feist                                      propertyPair.first == "ICoefficient" ||
4455b4aa86bSJames Feist                                      propertyPair.first == "ILimitMax" ||
4465b4aa86bSJames Feist                                      propertyPair.first == "ILimitMin" ||
447aad1a257SJames Feist                                      propertyPair.first ==
448aad1a257SJames Feist                                          "PositiveHysteresis" ||
449aad1a257SJames Feist                                      propertyPair.first ==
450aad1a257SJames Feist                                          "NegativeHysteresis" ||
4515b4aa86bSJames Feist                                      propertyPair.first == "OutLimitMax" ||
4525b4aa86bSJames Feist                                      propertyPair.first == "OutLimitMin" ||
4535b4aa86bSJames Feist                                      propertyPair.first == "PCoefficient" ||
4547625cb81SJames Feist                                      propertyPair.first == "SetPoint" ||
4555b4aa86bSJames Feist                                      propertyPair.first == "SlewNeg" ||
4565b4aa86bSJames Feist                                      propertyPair.first == "SlewPos")
4575b4aa86bSJames Feist                             {
4585b4aa86bSJames Feist                                 const double* ptr =
459abf2add6SEd Tanous                                     std::get_if<double>(&propertyPair.second);
4605b4aa86bSJames Feist                                 if (ptr == nullptr)
4615b4aa86bSJames Feist                                 {
4625b4aa86bSJames Feist                                     BMCWEB_LOG_ERROR << "Field Illegal "
4635b4aa86bSJames Feist                                                      << propertyPair.first;
464f12894f8SJason M. Bills                                     messages::internalError(asyncResp->res);
4655b4aa86bSJames Feist                                     return;
4665b4aa86bSJames Feist                                 }
467b7a08d04SJames Feist                                 (*config)[propertyPair.first] = *ptr;
4685b4aa86bSJames Feist                             }
4695b4aa86bSJames Feist                         }
4705b4aa86bSJames Feist                     }
4715b4aa86bSJames Feist                 }
4725b4aa86bSJames Feist             }
4735b4aa86bSJames Feist         },
4745b4aa86bSJames Feist         connection, path, objectManagerIface, "GetManagedObjects");
4755b4aa86bSJames Feist }
476ca537928SJennifer Lee 
47783ff9ab6SJames Feist enum class CreatePIDRet
47883ff9ab6SJames Feist {
47983ff9ab6SJames Feist     fail,
48083ff9ab6SJames Feist     del,
48183ff9ab6SJames Feist     patch
48283ff9ab6SJames Feist };
48383ff9ab6SJames Feist 
4845f2caaefSJames Feist static bool getZonesFromJsonReq(const std::shared_ptr<AsyncResp>& response,
4855f2caaefSJames Feist                                 std::vector<nlohmann::json>& config,
4865f2caaefSJames Feist                                 std::vector<std::string>& zones)
4875f2caaefSJames Feist {
488b6baeaa4SJames Feist     if (config.empty())
489b6baeaa4SJames Feist     {
490b6baeaa4SJames Feist         BMCWEB_LOG_ERROR << "Empty Zones";
491b6baeaa4SJames Feist         messages::propertyValueFormatError(response->res,
492b6baeaa4SJames Feist                                            nlohmann::json::array(), "Zones");
493b6baeaa4SJames Feist         return false;
494b6baeaa4SJames Feist     }
4955f2caaefSJames Feist     for (auto& odata : config)
4965f2caaefSJames Feist     {
4975f2caaefSJames Feist         std::string path;
4985f2caaefSJames Feist         if (!redfish::json_util::readJson(odata, response->res, "@odata.id",
4995f2caaefSJames Feist                                           path))
5005f2caaefSJames Feist         {
5015f2caaefSJames Feist             return false;
5025f2caaefSJames Feist         }
5035f2caaefSJames Feist         std::string input;
5045f2caaefSJames Feist         if (!dbus::utility::getNthStringFromPath(path, 4, input))
5055f2caaefSJames Feist         {
5065f2caaefSJames Feist             BMCWEB_LOG_ERROR << "Got invalid path " << path;
5075f2caaefSJames Feist             BMCWEB_LOG_ERROR << "Illegal Type Zones";
5085f2caaefSJames Feist             messages::propertyValueFormatError(response->res, odata.dump(),
5095f2caaefSJames Feist                                                "Zones");
5105f2caaefSJames Feist             return false;
5115f2caaefSJames Feist         }
5125f2caaefSJames Feist         boost::replace_all(input, "_", " ");
5135f2caaefSJames Feist         zones.emplace_back(std::move(input));
5145f2caaefSJames Feist     }
5155f2caaefSJames Feist     return true;
5165f2caaefSJames Feist }
5175f2caaefSJames Feist 
518b6baeaa4SJames Feist static bool findChassis(const dbus::utility::ManagedObjectType& managedObj,
519b6baeaa4SJames Feist                         const std::string& value, std::string& chassis)
520b6baeaa4SJames Feist {
521b6baeaa4SJames Feist     BMCWEB_LOG_DEBUG << "Find Chassis: " << value << "\n";
522b6baeaa4SJames Feist 
523b6baeaa4SJames Feist     std::string escaped = boost::replace_all_copy(value, " ", "_");
524b6baeaa4SJames Feist     escaped = "/" + escaped;
525b6baeaa4SJames Feist     auto it = std::find_if(
526b6baeaa4SJames Feist         managedObj.begin(), managedObj.end(), [&escaped](const auto& obj) {
527b6baeaa4SJames Feist             if (boost::algorithm::ends_with(obj.first.str, escaped))
528b6baeaa4SJames Feist             {
529b6baeaa4SJames Feist                 BMCWEB_LOG_DEBUG << "Matched " << obj.first.str << "\n";
530b6baeaa4SJames Feist                 return true;
531b6baeaa4SJames Feist             }
532b6baeaa4SJames Feist             return false;
533b6baeaa4SJames Feist         });
534b6baeaa4SJames Feist 
535b6baeaa4SJames Feist     if (it == managedObj.end())
536b6baeaa4SJames Feist     {
537b6baeaa4SJames Feist         return false;
538b6baeaa4SJames Feist     }
539b6baeaa4SJames Feist     // 5 comes from <chassis-name> being the 5th element
540b6baeaa4SJames Feist     // /xyz/openbmc_project/inventory/system/chassis/<chassis-name>
541b6baeaa4SJames Feist     return dbus::utility::getNthStringFromPath(it->first.str, 5, chassis);
542b6baeaa4SJames Feist }
543b6baeaa4SJames Feist 
54483ff9ab6SJames Feist static CreatePIDRet createPidInterface(
54583ff9ab6SJames Feist     const std::shared_ptr<AsyncResp>& response, const std::string& type,
546b6baeaa4SJames Feist     nlohmann::json::iterator it, const std::string& path,
54783ff9ab6SJames Feist     const dbus::utility::ManagedObjectType& managedObj, bool createNewObject,
54883ff9ab6SJames Feist     boost::container::flat_map<std::string, dbus::utility::DbusVariantType>&
54983ff9ab6SJames Feist         output,
55083ff9ab6SJames Feist     std::string& chassis)
55183ff9ab6SJames Feist {
55283ff9ab6SJames Feist 
5535f2caaefSJames Feist     // common deleter
554b6baeaa4SJames Feist     if (it.value() == nullptr)
5555f2caaefSJames Feist     {
5565f2caaefSJames Feist         std::string iface;
5575f2caaefSJames Feist         if (type == "PidControllers" || type == "FanControllers")
5585f2caaefSJames Feist         {
5595f2caaefSJames Feist             iface = pidConfigurationIface;
5605f2caaefSJames Feist         }
5615f2caaefSJames Feist         else if (type == "FanZones")
5625f2caaefSJames Feist         {
5635f2caaefSJames Feist             iface = pidZoneConfigurationIface;
5645f2caaefSJames Feist         }
5655f2caaefSJames Feist         else if (type == "StepwiseControllers")
5665f2caaefSJames Feist         {
5675f2caaefSJames Feist             iface = stepwiseConfigurationIface;
5685f2caaefSJames Feist         }
5695f2caaefSJames Feist         else
5705f2caaefSJames Feist         {
5715f2caaefSJames Feist             BMCWEB_LOG_ERROR << "Line:" << __LINE__ << ", Illegal Type "
5725f2caaefSJames Feist                              << type;
5735f2caaefSJames Feist             messages::propertyUnknown(response->res, type);
5745f2caaefSJames Feist             return CreatePIDRet::fail;
5755f2caaefSJames Feist         }
5765f2caaefSJames Feist         // delete interface
5775f2caaefSJames Feist         crow::connections::systemBus->async_method_call(
5785f2caaefSJames Feist             [response, path](const boost::system::error_code ec) {
5795f2caaefSJames Feist                 if (ec)
5805f2caaefSJames Feist                 {
5815f2caaefSJames Feist                     BMCWEB_LOG_ERROR << "Error patching " << path << ": " << ec;
5825f2caaefSJames Feist                     messages::internalError(response->res);
583b6baeaa4SJames Feist                     return;
5845f2caaefSJames Feist                 }
585b6baeaa4SJames Feist                 messages::success(response->res);
5865f2caaefSJames Feist             },
5875f2caaefSJames Feist             "xyz.openbmc_project.EntityManager", path, iface, "Delete");
5885f2caaefSJames Feist         return CreatePIDRet::del;
5895f2caaefSJames Feist     }
5905f2caaefSJames Feist 
591b6baeaa4SJames Feist     if (!createNewObject)
592b6baeaa4SJames Feist     {
593b6baeaa4SJames Feist         // if we aren't creating a new object, we should be able to find it on
594b6baeaa4SJames Feist         // d-bus
595b6baeaa4SJames Feist         if (!findChassis(managedObj, it.key(), chassis))
596b6baeaa4SJames Feist         {
597b6baeaa4SJames Feist             BMCWEB_LOG_ERROR << "Failed to get chassis from config patch";
598b6baeaa4SJames Feist             messages::invalidObject(response->res, it.key());
599b6baeaa4SJames Feist             return CreatePIDRet::fail;
600b6baeaa4SJames Feist         }
601b6baeaa4SJames Feist     }
602b6baeaa4SJames Feist 
60383ff9ab6SJames Feist     if (type == "PidControllers" || type == "FanControllers")
60483ff9ab6SJames Feist     {
60583ff9ab6SJames Feist         if (createNewObject)
60683ff9ab6SJames Feist         {
60783ff9ab6SJames Feist             output["Class"] = type == "PidControllers" ? std::string("temp")
60883ff9ab6SJames Feist                                                        : std::string("fan");
60983ff9ab6SJames Feist             output["Type"] = std::string("Pid");
61083ff9ab6SJames Feist         }
6115f2caaefSJames Feist 
6125f2caaefSJames Feist         std::optional<std::vector<nlohmann::json>> zones;
6135f2caaefSJames Feist         std::optional<std::vector<std::string>> inputs;
6145f2caaefSJames Feist         std::optional<std::vector<std::string>> outputs;
6155f2caaefSJames Feist         std::map<std::string, std::optional<double>> doubles;
6165f2caaefSJames Feist         if (!redfish::json_util::readJson(
617b6baeaa4SJames Feist                 it.value(), response->res, "Inputs", inputs, "Outputs", outputs,
6185f2caaefSJames Feist                 "Zones", zones, "FFGainCoefficient",
6195f2caaefSJames Feist                 doubles["FFGainCoefficient"], "FFOffCoefficient",
6205f2caaefSJames Feist                 doubles["FFOffCoefficient"], "ICoefficient",
6215f2caaefSJames Feist                 doubles["ICoefficient"], "ILimitMax", doubles["ILimitMax"],
6225f2caaefSJames Feist                 "ILimitMin", doubles["ILimitMin"], "OutLimitMax",
6235f2caaefSJames Feist                 doubles["OutLimitMax"], "OutLimitMin", doubles["OutLimitMin"],
6245f2caaefSJames Feist                 "PCoefficient", doubles["PCoefficient"], "SetPoint",
6255f2caaefSJames Feist                 doubles["SetPoint"], "SlewNeg", doubles["SlewNeg"], "SlewPos",
626aad1a257SJames Feist                 doubles["SlewPos"], "PositiveHysteresis",
627aad1a257SJames Feist                 doubles["PositiveHysteresis"], "NegativeHysteresis",
628aad1a257SJames Feist                 doubles["NegativeHysteresis"]))
62983ff9ab6SJames Feist         {
6305f2caaefSJames Feist             BMCWEB_LOG_ERROR << "Line:" << __LINE__ << ", Illegal Property "
631b6baeaa4SJames Feist                              << it.value().dump();
6325f2caaefSJames Feist             return CreatePIDRet::fail;
63383ff9ab6SJames Feist         }
6345f2caaefSJames Feist         if (zones)
6355f2caaefSJames Feist         {
6365f2caaefSJames Feist             std::vector<std::string> zonesStr;
6375f2caaefSJames Feist             if (!getZonesFromJsonReq(response, *zones, zonesStr))
6385f2caaefSJames Feist             {
6395f2caaefSJames Feist                 BMCWEB_LOG_ERROR << "Line:" << __LINE__ << ", Illegal Zones";
6405f2caaefSJames Feist                 return CreatePIDRet::fail;
6415f2caaefSJames Feist             }
642b6baeaa4SJames Feist             if (chassis.empty() &&
643b6baeaa4SJames Feist                 !findChassis(managedObj, zonesStr[0], chassis))
644b6baeaa4SJames Feist             {
645b6baeaa4SJames Feist                 BMCWEB_LOG_ERROR << "Failed to get chassis from config patch";
646b6baeaa4SJames Feist                 messages::invalidObject(response->res, it.key());
647b6baeaa4SJames Feist                 return CreatePIDRet::fail;
648b6baeaa4SJames Feist             }
649b6baeaa4SJames Feist 
6505f2caaefSJames Feist             output["Zones"] = std::move(zonesStr);
6515f2caaefSJames Feist         }
6525f2caaefSJames Feist         if (inputs || outputs)
6535f2caaefSJames Feist         {
6545f2caaefSJames Feist             std::array<std::optional<std::vector<std::string>>*, 2> containers =
6555f2caaefSJames Feist                 {&inputs, &outputs};
6565f2caaefSJames Feist             size_t index = 0;
6575f2caaefSJames Feist             for (const auto& containerPtr : containers)
6585f2caaefSJames Feist             {
6595f2caaefSJames Feist                 std::optional<std::vector<std::string>>& container =
6605f2caaefSJames Feist                     *containerPtr;
6615f2caaefSJames Feist                 if (!container)
6625f2caaefSJames Feist                 {
6635f2caaefSJames Feist                     index++;
6645f2caaefSJames Feist                     continue;
66583ff9ab6SJames Feist                 }
66683ff9ab6SJames Feist 
6675f2caaefSJames Feist                 for (std::string& value : *container)
66883ff9ab6SJames Feist                 {
6695f2caaefSJames Feist                     boost::replace_all(value, "_", " ");
67083ff9ab6SJames Feist                 }
6715f2caaefSJames Feist                 std::string key;
6725f2caaefSJames Feist                 if (index == 0)
6735f2caaefSJames Feist                 {
6745f2caaefSJames Feist                     key = "Inputs";
6755f2caaefSJames Feist                 }
6765f2caaefSJames Feist                 else
6775f2caaefSJames Feist                 {
6785f2caaefSJames Feist                     key = "Outputs";
6795f2caaefSJames Feist                 }
6805f2caaefSJames Feist                 output[key] = *container;
6815f2caaefSJames Feist                 index++;
6825f2caaefSJames Feist             }
68383ff9ab6SJames Feist         }
68483ff9ab6SJames Feist 
68583ff9ab6SJames Feist         // doubles
6865f2caaefSJames Feist         for (const auto& pairs : doubles)
68783ff9ab6SJames Feist         {
6885f2caaefSJames Feist             if (!pairs.second)
68983ff9ab6SJames Feist             {
6905f2caaefSJames Feist                 continue;
69183ff9ab6SJames Feist             }
6925f2caaefSJames Feist             BMCWEB_LOG_DEBUG << pairs.first << " = " << *pairs.second;
6935f2caaefSJames Feist             output[pairs.first] = *(pairs.second);
6945f2caaefSJames Feist         }
69583ff9ab6SJames Feist     }
69683ff9ab6SJames Feist 
69783ff9ab6SJames Feist     else if (type == "FanZones")
69883ff9ab6SJames Feist     {
69983ff9ab6SJames Feist         output["Type"] = std::string("Pid.Zone");
70083ff9ab6SJames Feist 
7015f2caaefSJames Feist         std::optional<nlohmann::json> chassisContainer;
7025f2caaefSJames Feist         std::optional<double> failSafePercent;
703d3ec07f8SJames Feist         std::optional<double> minThermalOutput;
704b6baeaa4SJames Feist         if (!redfish::json_util::readJson(it.value(), response->res, "Chassis",
7055f2caaefSJames Feist                                           chassisContainer, "FailSafePercent",
706d3ec07f8SJames Feist                                           failSafePercent, "MinThermalOutput",
707d3ec07f8SJames Feist                                           minThermalOutput))
70883ff9ab6SJames Feist         {
7095f2caaefSJames Feist             BMCWEB_LOG_ERROR << "Line:" << __LINE__ << ", Illegal Property "
710b6baeaa4SJames Feist                              << it.value().dump();
71183ff9ab6SJames Feist             return CreatePIDRet::fail;
71283ff9ab6SJames Feist         }
7135f2caaefSJames Feist 
7145f2caaefSJames Feist         if (chassisContainer)
71583ff9ab6SJames Feist         {
7165f2caaefSJames Feist 
7175f2caaefSJames Feist             std::string chassisId;
7185f2caaefSJames Feist             if (!redfish::json_util::readJson(*chassisContainer, response->res,
7195f2caaefSJames Feist                                               "@odata.id", chassisId))
7205f2caaefSJames Feist             {
7215f2caaefSJames Feist                 BMCWEB_LOG_ERROR << "Line:" << __LINE__ << ", Illegal Property "
7225f2caaefSJames Feist                                  << chassisContainer->dump();
72383ff9ab6SJames Feist                 return CreatePIDRet::fail;
72483ff9ab6SJames Feist             }
72583ff9ab6SJames Feist 
72683ff9ab6SJames Feist             // /refish/v1/chassis/chassis_name/
7275f2caaefSJames Feist             if (!dbus::utility::getNthStringFromPath(chassisId, 3, chassis))
72883ff9ab6SJames Feist             {
7295f2caaefSJames Feist                 BMCWEB_LOG_ERROR << "Got invalid path " << chassisId;
7305f2caaefSJames Feist                 messages::invalidObject(response->res, chassisId);
73183ff9ab6SJames Feist                 return CreatePIDRet::fail;
73283ff9ab6SJames Feist             }
73383ff9ab6SJames Feist         }
734d3ec07f8SJames Feist         if (minThermalOutput)
73583ff9ab6SJames Feist         {
736d3ec07f8SJames Feist             output["MinThermalOutput"] = *minThermalOutput;
7375f2caaefSJames Feist         }
7385f2caaefSJames Feist         if (failSafePercent)
73983ff9ab6SJames Feist         {
7405f2caaefSJames Feist             output["FailSafePercent"] = *failSafePercent;
7415f2caaefSJames Feist         }
7425f2caaefSJames Feist     }
7435f2caaefSJames Feist     else if (type == "StepwiseControllers")
7445f2caaefSJames Feist     {
7455f2caaefSJames Feist         output["Type"] = std::string("Stepwise");
7465f2caaefSJames Feist 
7475f2caaefSJames Feist         std::optional<std::vector<nlohmann::json>> zones;
7485f2caaefSJames Feist         std::optional<std::vector<nlohmann::json>> steps;
7495f2caaefSJames Feist         std::optional<std::vector<std::string>> inputs;
7505f2caaefSJames Feist         std::optional<double> positiveHysteresis;
7515f2caaefSJames Feist         std::optional<double> negativeHysteresis;
752*c33a90ecSJames Feist         std::optional<std::string> direction; // upper clipping curve vs lower
7535f2caaefSJames Feist         if (!redfish::json_util::readJson(
754b6baeaa4SJames Feist                 it.value(), response->res, "Zones", zones, "Steps", steps,
755b6baeaa4SJames Feist                 "Inputs", inputs, "PositiveHysteresis", positiveHysteresis,
756*c33a90ecSJames Feist                 "NegativeHysteresis", negativeHysteresis, "Direction",
757*c33a90ecSJames Feist                 direction))
7585f2caaefSJames Feist         {
7595f2caaefSJames Feist             BMCWEB_LOG_ERROR << "Line:" << __LINE__ << ", Illegal Property "
760b6baeaa4SJames Feist                              << it.value().dump();
76183ff9ab6SJames Feist             return CreatePIDRet::fail;
76283ff9ab6SJames Feist         }
7635f2caaefSJames Feist 
7645f2caaefSJames Feist         if (zones)
76583ff9ab6SJames Feist         {
766b6baeaa4SJames Feist             std::vector<std::string> zonesStrs;
767b6baeaa4SJames Feist             if (!getZonesFromJsonReq(response, *zones, zonesStrs))
7685f2caaefSJames Feist             {
7695f2caaefSJames Feist                 BMCWEB_LOG_ERROR << "Line:" << __LINE__ << ", Illegal Zones";
77083ff9ab6SJames Feist                 return CreatePIDRet::fail;
77183ff9ab6SJames Feist             }
772b6baeaa4SJames Feist             if (chassis.empty() &&
773b6baeaa4SJames Feist                 !findChassis(managedObj, zonesStrs[0], chassis))
774b6baeaa4SJames Feist             {
775b6baeaa4SJames Feist                 BMCWEB_LOG_ERROR << "Failed to get chassis from config patch";
776b6baeaa4SJames Feist                 messages::invalidObject(response->res, it.key());
777b6baeaa4SJames Feist                 return CreatePIDRet::fail;
778b6baeaa4SJames Feist             }
779b6baeaa4SJames Feist             output["Zones"] = std::move(zonesStrs);
7805f2caaefSJames Feist         }
7815f2caaefSJames Feist         if (steps)
7825f2caaefSJames Feist         {
7835f2caaefSJames Feist             std::vector<double> readings;
7845f2caaefSJames Feist             std::vector<double> outputs;
7855f2caaefSJames Feist             for (auto& step : *steps)
7865f2caaefSJames Feist             {
7875f2caaefSJames Feist                 double target;
7885f2caaefSJames Feist                 double output;
7895f2caaefSJames Feist 
7905f2caaefSJames Feist                 if (!redfish::json_util::readJson(step, response->res, "Target",
7915f2caaefSJames Feist                                                   target, "Output", output))
7925f2caaefSJames Feist                 {
7935f2caaefSJames Feist                     BMCWEB_LOG_ERROR << "Line:" << __LINE__
794b6baeaa4SJames Feist                                      << ", Illegal Property "
795b6baeaa4SJames Feist                                      << it.value().dump();
7965f2caaefSJames Feist                     return CreatePIDRet::fail;
7975f2caaefSJames Feist                 }
7985f2caaefSJames Feist                 readings.emplace_back(target);
7995f2caaefSJames Feist                 outputs.emplace_back(output);
8005f2caaefSJames Feist             }
8015f2caaefSJames Feist             output["Reading"] = std::move(readings);
8025f2caaefSJames Feist             output["Output"] = std::move(outputs);
8035f2caaefSJames Feist         }
8045f2caaefSJames Feist         if (inputs)
8055f2caaefSJames Feist         {
8065f2caaefSJames Feist             for (std::string& value : *inputs)
8075f2caaefSJames Feist             {
8085f2caaefSJames Feist                 boost::replace_all(value, "_", " ");
8095f2caaefSJames Feist             }
8105f2caaefSJames Feist             output["Inputs"] = std::move(*inputs);
8115f2caaefSJames Feist         }
8125f2caaefSJames Feist         if (negativeHysteresis)
8135f2caaefSJames Feist         {
8145f2caaefSJames Feist             output["NegativeHysteresis"] = *negativeHysteresis;
8155f2caaefSJames Feist         }
8165f2caaefSJames Feist         if (positiveHysteresis)
8175f2caaefSJames Feist         {
8185f2caaefSJames Feist             output["PositiveHysteresis"] = *positiveHysteresis;
81983ff9ab6SJames Feist         }
820*c33a90ecSJames Feist         if (direction)
821*c33a90ecSJames Feist         {
822*c33a90ecSJames Feist             constexpr const std::array<const char*, 2> allowedDirections = {
823*c33a90ecSJames Feist                 "Ceiling", "Floor"};
824*c33a90ecSJames Feist             if (std::find(allowedDirections.begin(), allowedDirections.end(),
825*c33a90ecSJames Feist                           *direction) == allowedDirections.end())
826*c33a90ecSJames Feist             {
827*c33a90ecSJames Feist                 messages::propertyValueTypeError(response->res, "Direction",
828*c33a90ecSJames Feist                                                  *direction);
829*c33a90ecSJames Feist                 return CreatePIDRet::fail;
830*c33a90ecSJames Feist             }
831*c33a90ecSJames Feist             output["Class"] = *direction;
832*c33a90ecSJames Feist         }
83383ff9ab6SJames Feist     }
83483ff9ab6SJames Feist     else
83583ff9ab6SJames Feist     {
8365f2caaefSJames Feist         BMCWEB_LOG_ERROR << "Line:" << __LINE__ << ", Illegal Type " << type;
83735a62c7cSJason M. Bills         messages::propertyUnknown(response->res, type);
83883ff9ab6SJames Feist         return CreatePIDRet::fail;
83983ff9ab6SJames Feist     }
84083ff9ab6SJames Feist     return CreatePIDRet::patch;
84183ff9ab6SJames Feist }
84283ff9ab6SJames Feist 
8431abe55efSEd Tanous class Manager : public Node
8441abe55efSEd Tanous {
8459c310685SBorawski.Lukasz   public:
8465b4aa86bSJames Feist     Manager(CrowApp& app) : Node(app, "/redfish/v1/Managers/bmc/")
8471abe55efSEd Tanous     {
8480f74e643SEd Tanous         uuid = app.template getMiddleware<crow::persistent_data::Middleware>()
84955c7b7a2SEd Tanous                    .systemUuid;
850a434f2bdSEd Tanous         entityPrivileges = {
851a434f2bdSEd Tanous             {boost::beast::http::verb::get, {{"Login"}}},
852e0d918bcSEd Tanous             {boost::beast::http::verb::head, {{"Login"}}},
853e0d918bcSEd Tanous             {boost::beast::http::verb::patch, {{"ConfigureManager"}}},
854e0d918bcSEd Tanous             {boost::beast::http::verb::put, {{"ConfigureManager"}}},
855e0d918bcSEd Tanous             {boost::beast::http::verb::delete_, {{"ConfigureManager"}}},
856e0d918bcSEd Tanous             {boost::beast::http::verb::post, {{"ConfigureManager"}}}};
8579c310685SBorawski.Lukasz     }
8589c310685SBorawski.Lukasz 
8599c310685SBorawski.Lukasz   private:
8605b4aa86bSJames Feist     void getPidValues(std::shared_ptr<AsyncResp> asyncResp)
8615b4aa86bSJames Feist     {
8625b4aa86bSJames Feist         crow::connections::systemBus->async_method_call(
8635b4aa86bSJames Feist             [asyncResp](const boost::system::error_code ec,
8645b4aa86bSJames Feist                         const crow::openbmc_mapper::GetSubTreeType& subtree) {
8655b4aa86bSJames Feist                 if (ec)
8665b4aa86bSJames Feist                 {
8675b4aa86bSJames Feist                     BMCWEB_LOG_ERROR << ec;
868f12894f8SJason M. Bills                     messages::internalError(asyncResp->res);
8695b4aa86bSJames Feist                     return;
8705b4aa86bSJames Feist                 }
8715b4aa86bSJames Feist 
8725b4aa86bSJames Feist                 // create map of <connection, path to objMgr>>
8735b4aa86bSJames Feist                 boost::container::flat_map<std::string, std::string>
8745b4aa86bSJames Feist                     objectMgrPaths;
8756bce33bcSJames Feist                 boost::container::flat_set<std::string> calledConnections;
8765b4aa86bSJames Feist                 for (const auto& pathGroup : subtree)
8775b4aa86bSJames Feist                 {
8785b4aa86bSJames Feist                     for (const auto& connectionGroup : pathGroup.second)
8795b4aa86bSJames Feist                     {
8806bce33bcSJames Feist                         auto findConnection =
8816bce33bcSJames Feist                             calledConnections.find(connectionGroup.first);
8826bce33bcSJames Feist                         if (findConnection != calledConnections.end())
8836bce33bcSJames Feist                         {
8846bce33bcSJames Feist                             break;
8856bce33bcSJames Feist                         }
8865b4aa86bSJames Feist                         for (const std::string& interface :
8875b4aa86bSJames Feist                              connectionGroup.second)
8885b4aa86bSJames Feist                         {
8895b4aa86bSJames Feist                             if (interface == objectManagerIface)
8905b4aa86bSJames Feist                             {
8915b4aa86bSJames Feist                                 objectMgrPaths[connectionGroup.first] =
8925b4aa86bSJames Feist                                     pathGroup.first;
8935b4aa86bSJames Feist                             }
8945b4aa86bSJames Feist                             // this list is alphabetical, so we
8955b4aa86bSJames Feist                             // should have found the objMgr by now
8965b4aa86bSJames Feist                             if (interface == pidConfigurationIface ||
897b7a08d04SJames Feist                                 interface == pidZoneConfigurationIface ||
898b7a08d04SJames Feist                                 interface == stepwiseConfigurationIface)
8995b4aa86bSJames Feist                             {
9005b4aa86bSJames Feist                                 auto findObjMgr =
9015b4aa86bSJames Feist                                     objectMgrPaths.find(connectionGroup.first);
9025b4aa86bSJames Feist                                 if (findObjMgr == objectMgrPaths.end())
9035b4aa86bSJames Feist                                 {
9045b4aa86bSJames Feist                                     BMCWEB_LOG_DEBUG << connectionGroup.first
9055b4aa86bSJames Feist                                                      << "Has no Object Manager";
9065b4aa86bSJames Feist                                     continue;
9075b4aa86bSJames Feist                                 }
9086bce33bcSJames Feist 
9096bce33bcSJames Feist                                 calledConnections.insert(connectionGroup.first);
9106bce33bcSJames Feist 
9115b4aa86bSJames Feist                                 asyncPopulatePid(findObjMgr->first,
9125b4aa86bSJames Feist                                                  findObjMgr->second, asyncResp);
9135b4aa86bSJames Feist                                 break;
9145b4aa86bSJames Feist                             }
9155b4aa86bSJames Feist                         }
9165b4aa86bSJames Feist                     }
9175b4aa86bSJames Feist                 }
9185b4aa86bSJames Feist             },
9195b4aa86bSJames Feist             "xyz.openbmc_project.ObjectMapper",
9205b4aa86bSJames Feist             "/xyz/openbmc_project/object_mapper",
9215b4aa86bSJames Feist             "xyz.openbmc_project.ObjectMapper", "GetSubTree", "/", 0,
922b7a08d04SJames Feist             std::array<const char*, 4>{
923b7a08d04SJames Feist                 pidConfigurationIface, pidZoneConfigurationIface,
924b7a08d04SJames Feist                 objectManagerIface, stepwiseConfigurationIface});
9255b4aa86bSJames Feist     }
9265b4aa86bSJames Feist 
92755c7b7a2SEd Tanous     void doGet(crow::Response& res, const crow::Request& req,
9281abe55efSEd Tanous                const std::vector<std::string>& params) override
9291abe55efSEd Tanous     {
9300f74e643SEd Tanous         res.jsonValue["@odata.id"] = "/redfish/v1/Managers/bmc";
9310f74e643SEd Tanous         res.jsonValue["@odata.type"] = "#Manager.v1_3_0.Manager";
9320f74e643SEd Tanous         res.jsonValue["@odata.context"] =
9330f74e643SEd Tanous             "/redfish/v1/$metadata#Manager.Manager";
9340f74e643SEd Tanous         res.jsonValue["Id"] = "bmc";
9350f74e643SEd Tanous         res.jsonValue["Name"] = "OpenBmc Manager";
9360f74e643SEd Tanous         res.jsonValue["Description"] = "Baseboard Management Controller";
9370f74e643SEd Tanous         res.jsonValue["PowerState"] = "On";
938029573d4SEd Tanous         res.jsonValue["Status"] = {{"State", "Enabled"}, {"Health", "OK"}};
9390f74e643SEd Tanous         res.jsonValue["ManagerType"] = "BMC";
9407517658eSEd Tanous         res.jsonValue["UUID"] = uuid;
9410f74e643SEd Tanous         res.jsonValue["Model"] = "OpenBmc"; // TODO(ed), get model
9420f74e643SEd Tanous 
9430f74e643SEd Tanous         res.jsonValue["LogServices"] = {
9440f74e643SEd Tanous             {"@odata.id", "/redfish/v1/Managers/bmc/LogServices"}};
9450f74e643SEd Tanous 
9460f74e643SEd Tanous         res.jsonValue["NetworkProtocol"] = {
9470f74e643SEd Tanous             {"@odata.id", "/redfish/v1/Managers/bmc/NetworkProtocol"}};
9480f74e643SEd Tanous 
9490f74e643SEd Tanous         res.jsonValue["EthernetInterfaces"] = {
9500f74e643SEd Tanous             {"@odata.id", "/redfish/v1/Managers/bmc/EthernetInterfaces"}};
9510f74e643SEd Tanous         // default oem data
9520f74e643SEd Tanous         nlohmann::json& oem = res.jsonValue["Oem"];
9530f74e643SEd Tanous         nlohmann::json& oemOpenbmc = oem["OpenBmc"];
9540f74e643SEd Tanous         oem["@odata.type"] = "#OemManager.Oem";
9550f74e643SEd Tanous         oem["@odata.id"] = "/redfish/v1/Managers/bmc#/Oem";
9560f74e643SEd Tanous         oem["@odata.context"] = "/redfish/v1/$metadata#OemManager.Oem";
9570f74e643SEd Tanous         oemOpenbmc["@odata.type"] = "#OemManager.OpenBmc";
9580f74e643SEd Tanous         oemOpenbmc["@odata.id"] = "/redfish/v1/Managers/bmc#/Oem/OpenBmc";
9590f74e643SEd Tanous         oemOpenbmc["@odata.context"] =
9600f74e643SEd Tanous             "/redfish/v1/$metadata#OemManager.OpenBmc";
9610f74e643SEd Tanous 
962ed5befbdSJennifer Lee         // Update Actions object.
9630f74e643SEd Tanous         nlohmann::json& manager_reset =
9640f74e643SEd Tanous             res.jsonValue["Actions"]["#Manager.Reset"];
965ed5befbdSJennifer Lee         manager_reset["target"] =
966ed5befbdSJennifer Lee             "/redfish/v1/Managers/bmc/Actions/Manager.Reset";
967ed5befbdSJennifer Lee         manager_reset["ResetType@Redfish.AllowableValues"] = {
968ed5befbdSJennifer Lee             "GracefulRestart"};
969ca537928SJennifer Lee 
9700f74e643SEd Tanous         res.jsonValue["DateTime"] = getDateTime();
971029573d4SEd Tanous         res.jsonValue["Links"] = {
972029573d4SEd Tanous             {"ManagerForServers@odata.count", 1},
973029573d4SEd Tanous             {"ManagerForServers",
974029573d4SEd Tanous              {{{"@odata.id", "/redfish/v1/Systems/system"}}}},
975029573d4SEd Tanous             {"ManagerForServers", nlohmann::json::array()}};
976ed5befbdSJennifer Lee         std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
9775b4aa86bSJames Feist 
978ca537928SJennifer Lee         crow::connections::systemBus->async_method_call(
979ca537928SJennifer Lee             [asyncResp](const boost::system::error_code ec,
9805b4aa86bSJames Feist                         const dbus::utility::ManagedObjectType& resp) {
981ca537928SJennifer Lee                 if (ec)
982ca537928SJennifer Lee                 {
983ca537928SJennifer Lee                     BMCWEB_LOG_ERROR << "Error while getting Software Version";
984f12894f8SJason M. Bills                     messages::internalError(asyncResp->res);
985ca537928SJennifer Lee                     return;
986ca537928SJennifer Lee                 }
987ca537928SJennifer Lee 
988ca537928SJennifer Lee                 for (auto& objpath : resp)
989ca537928SJennifer Lee                 {
990ca537928SJennifer Lee                     for (auto& interface : objpath.second)
991ca537928SJennifer Lee                     {
9925f2caaefSJames Feist                         // If interface is
9935f2caaefSJames Feist                         // xyz.openbmc_project.Software.Version, this is
9945f2caaefSJames Feist                         // what we're looking for.
995ca537928SJennifer Lee                         if (interface.first ==
996ca537928SJennifer Lee                             "xyz.openbmc_project.Software.Version")
997ca537928SJennifer Lee                         {
998ca537928SJennifer Lee                             // Cut out everyting until last "/", ...
999ca537928SJennifer Lee                             for (auto& property : interface.second)
1000ca537928SJennifer Lee                             {
1001ca537928SJennifer Lee                                 if (property.first == "Version")
1002ca537928SJennifer Lee                                 {
1003ca537928SJennifer Lee                                     const std::string* value =
1004abf2add6SEd Tanous                                         std::get_if<std::string>(
1005abf2add6SEd Tanous                                             &property.second);
1006ca537928SJennifer Lee                                     if (value == nullptr)
1007ca537928SJennifer Lee                                     {
1008ca537928SJennifer Lee                                         continue;
1009ca537928SJennifer Lee                                     }
1010ca537928SJennifer Lee                                     asyncResp->res
1011ca537928SJennifer Lee                                         .jsonValue["FirmwareVersion"] = *value;
1012ca537928SJennifer Lee                                 }
1013ca537928SJennifer Lee                             }
1014ca537928SJennifer Lee                         }
1015ca537928SJennifer Lee                     }
1016ca537928SJennifer Lee                 }
1017ca537928SJennifer Lee             },
1018ca537928SJennifer Lee             "xyz.openbmc_project.Software.BMC.Updater",
1019ca537928SJennifer Lee             "/xyz/openbmc_project/software",
1020ca537928SJennifer Lee             "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
10215b4aa86bSJames Feist         getPidValues(asyncResp);
10225b4aa86bSJames Feist     }
10235f2caaefSJames Feist     void setPidValues(std::shared_ptr<AsyncResp> response, nlohmann::json& data)
102483ff9ab6SJames Feist     {
10255f2caaefSJames Feist 
102683ff9ab6SJames Feist         // todo(james): might make sense to do a mapper call here if this
102783ff9ab6SJames Feist         // interface gets more traction
102883ff9ab6SJames Feist         crow::connections::systemBus->async_method_call(
102983ff9ab6SJames Feist             [response,
103083ff9ab6SJames Feist              data](const boost::system::error_code ec,
103183ff9ab6SJames Feist                    const dbus::utility::ManagedObjectType& managedObj) {
103283ff9ab6SJames Feist                 if (ec)
103383ff9ab6SJames Feist                 {
103483ff9ab6SJames Feist                     BMCWEB_LOG_ERROR << "Error communicating to Entity Manager";
103535a62c7cSJason M. Bills                     messages::internalError(response->res);
103683ff9ab6SJames Feist                     return;
103783ff9ab6SJames Feist                 }
10385f2caaefSJames Feist 
10395f2caaefSJames Feist                 // todo(james) mutable doesn't work with asio bindings
10405f2caaefSJames Feist                 nlohmann::json jsonData = data;
10415f2caaefSJames Feist 
10425f2caaefSJames Feist                 std::optional<nlohmann::json> pidControllers;
10435f2caaefSJames Feist                 std::optional<nlohmann::json> fanControllers;
10445f2caaefSJames Feist                 std::optional<nlohmann::json> fanZones;
10455f2caaefSJames Feist                 std::optional<nlohmann::json> stepwiseControllers;
10465f2caaefSJames Feist                 if (!redfish::json_util::readJson(
10475f2caaefSJames Feist                         jsonData, response->res, "PidControllers",
10485f2caaefSJames Feist                         pidControllers, "FanControllers", fanControllers,
10495f2caaefSJames Feist                         "FanZones", fanZones, "StepwiseControllers",
10505f2caaefSJames Feist                         stepwiseControllers))
105183ff9ab6SJames Feist                 {
10525f2caaefSJames Feist                     BMCWEB_LOG_ERROR << "Line:" << __LINE__
10535f2caaefSJames Feist                                      << ", Illegal Property "
10545f2caaefSJames Feist                                      << jsonData.dump();
105583ff9ab6SJames Feist                     return;
105683ff9ab6SJames Feist                 }
10575f2caaefSJames Feist                 std::array<
105843b761d0SEd Tanous                     std::pair<std::string, std::optional<nlohmann::json>*>, 4>
10595f2caaefSJames Feist                     sections = {
10605f2caaefSJames Feist                         std::make_pair("PidControllers", &pidControllers),
10615f2caaefSJames Feist                         std::make_pair("FanControllers", &fanControllers),
10625f2caaefSJames Feist                         std::make_pair("FanZones", &fanZones),
10635f2caaefSJames Feist                         std::make_pair("StepwiseControllers",
10645f2caaefSJames Feist                                        &stepwiseControllers)};
10655f2caaefSJames Feist 
10665f2caaefSJames Feist                 for (auto& containerPair : sections)
106783ff9ab6SJames Feist                 {
10685f2caaefSJames Feist                     auto& container = *(containerPair.second);
10695f2caaefSJames Feist                     if (!container)
10705f2caaefSJames Feist                     {
10715f2caaefSJames Feist                         continue;
10725f2caaefSJames Feist                     }
107343b761d0SEd Tanous                     std::string& type = containerPair.first;
10745f2caaefSJames Feist 
1075b6baeaa4SJames Feist                     for (nlohmann::json::iterator it = container->begin();
1076b6baeaa4SJames Feist                          it != container->end(); it++)
10775f2caaefSJames Feist                     {
1078b6baeaa4SJames Feist                         const auto& name = it.key();
107983ff9ab6SJames Feist                         auto pathItr =
108083ff9ab6SJames Feist                             std::find_if(managedObj.begin(), managedObj.end(),
108183ff9ab6SJames Feist                                          [&name](const auto& obj) {
108283ff9ab6SJames Feist                                              return boost::algorithm::ends_with(
1083b6baeaa4SJames Feist                                                  obj.first.str, "/" + name);
108483ff9ab6SJames Feist                                          });
108583ff9ab6SJames Feist                         boost::container::flat_map<
108683ff9ab6SJames Feist                             std::string, dbus::utility::DbusVariantType>
108783ff9ab6SJames Feist                             output;
108883ff9ab6SJames Feist 
108983ff9ab6SJames Feist                         output.reserve(16); // The pid interface length
109083ff9ab6SJames Feist 
109183ff9ab6SJames Feist                         // determines if we're patching entity-manager or
109283ff9ab6SJames Feist                         // creating a new object
109383ff9ab6SJames Feist                         bool createNewObject = (pathItr == managedObj.end());
10945f2caaefSJames Feist                         std::string iface;
10955f2caaefSJames Feist                         if (type == "PidControllers" ||
10965f2caaefSJames Feist                             type == "FanControllers")
109783ff9ab6SJames Feist                         {
10985f2caaefSJames Feist                             iface = pidConfigurationIface;
109983ff9ab6SJames Feist                             if (!createNewObject &&
110083ff9ab6SJames Feist                                 pathItr->second.find(pidConfigurationIface) ==
110183ff9ab6SJames Feist                                     pathItr->second.end())
110283ff9ab6SJames Feist                             {
110383ff9ab6SJames Feist                                 createNewObject = true;
110483ff9ab6SJames Feist                             }
110583ff9ab6SJames Feist                         }
11065f2caaefSJames Feist                         else if (type == "FanZones")
11075f2caaefSJames Feist                         {
11085f2caaefSJames Feist                             iface = pidZoneConfigurationIface;
11095f2caaefSJames Feist                             if (!createNewObject &&
111083ff9ab6SJames Feist                                 pathItr->second.find(
111183ff9ab6SJames Feist                                     pidZoneConfigurationIface) ==
111283ff9ab6SJames Feist                                     pathItr->second.end())
111383ff9ab6SJames Feist                             {
11145f2caaefSJames Feist 
111583ff9ab6SJames Feist                                 createNewObject = true;
111683ff9ab6SJames Feist                             }
11175f2caaefSJames Feist                         }
11185f2caaefSJames Feist                         else if (type == "StepwiseControllers")
11195f2caaefSJames Feist                         {
11205f2caaefSJames Feist                             iface = stepwiseConfigurationIface;
11215f2caaefSJames Feist                             if (!createNewObject &&
11225f2caaefSJames Feist                                 pathItr->second.find(
11235f2caaefSJames Feist                                     stepwiseConfigurationIface) ==
11245f2caaefSJames Feist                                     pathItr->second.end())
11255f2caaefSJames Feist                             {
11265f2caaefSJames Feist                                 createNewObject = true;
11275f2caaefSJames Feist                             }
11285f2caaefSJames Feist                         }
1129b6baeaa4SJames Feist                         BMCWEB_LOG_DEBUG << "Create new = " << createNewObject
1130b6baeaa4SJames Feist                                          << "\n";
113183ff9ab6SJames Feist                         output["Name"] =
113283ff9ab6SJames Feist                             boost::replace_all_copy(name, "_", " ");
113383ff9ab6SJames Feist 
113483ff9ab6SJames Feist                         std::string chassis;
113583ff9ab6SJames Feist                         CreatePIDRet ret = createPidInterface(
1136b6baeaa4SJames Feist                             response, type, it, pathItr->first.str, managedObj,
1137b6baeaa4SJames Feist                             createNewObject, output, chassis);
113883ff9ab6SJames Feist                         if (ret == CreatePIDRet::fail)
113983ff9ab6SJames Feist                         {
114083ff9ab6SJames Feist                             return;
114183ff9ab6SJames Feist                         }
114283ff9ab6SJames Feist                         else if (ret == CreatePIDRet::del)
114383ff9ab6SJames Feist                         {
114483ff9ab6SJames Feist                             continue;
114583ff9ab6SJames Feist                         }
114683ff9ab6SJames Feist 
114783ff9ab6SJames Feist                         if (!createNewObject)
114883ff9ab6SJames Feist                         {
114983ff9ab6SJames Feist                             for (const auto& property : output)
115083ff9ab6SJames Feist                             {
115183ff9ab6SJames Feist                                 crow::connections::systemBus->async_method_call(
115283ff9ab6SJames Feist                                     [response,
115383ff9ab6SJames Feist                                      propertyName{std::string(property.first)}](
115483ff9ab6SJames Feist                                         const boost::system::error_code ec) {
115583ff9ab6SJames Feist                                         if (ec)
115683ff9ab6SJames Feist                                         {
115783ff9ab6SJames Feist                                             BMCWEB_LOG_ERROR
115883ff9ab6SJames Feist                                                 << "Error patching "
115983ff9ab6SJames Feist                                                 << propertyName << ": " << ec;
116035a62c7cSJason M. Bills                                             messages::internalError(
116135a62c7cSJason M. Bills                                                 response->res);
1162b6baeaa4SJames Feist                                             return;
116383ff9ab6SJames Feist                                         }
1164b6baeaa4SJames Feist                                         messages::success(response->res);
116583ff9ab6SJames Feist                                     },
116683ff9ab6SJames Feist                                     "xyz.openbmc_project.EntityManager",
116783ff9ab6SJames Feist                                     pathItr->first.str,
116883ff9ab6SJames Feist                                     "org.freedesktop.DBus.Properties", "Set",
11695f2caaefSJames Feist                                     iface, property.first, property.second);
117083ff9ab6SJames Feist                             }
117183ff9ab6SJames Feist                         }
117283ff9ab6SJames Feist                         else
117383ff9ab6SJames Feist                         {
117483ff9ab6SJames Feist                             if (chassis.empty())
117583ff9ab6SJames Feist                             {
117683ff9ab6SJames Feist                                 BMCWEB_LOG_ERROR
117783ff9ab6SJames Feist                                     << "Failed to get chassis from config";
117835a62c7cSJason M. Bills                                 messages::invalidObject(response->res, name);
117983ff9ab6SJames Feist                                 return;
118083ff9ab6SJames Feist                             }
118183ff9ab6SJames Feist 
118283ff9ab6SJames Feist                             bool foundChassis = false;
118383ff9ab6SJames Feist                             for (const auto& obj : managedObj)
118483ff9ab6SJames Feist                             {
118583ff9ab6SJames Feist                                 if (boost::algorithm::ends_with(obj.first.str,
118683ff9ab6SJames Feist                                                                 chassis))
118783ff9ab6SJames Feist                                 {
118883ff9ab6SJames Feist                                     chassis = obj.first.str;
118983ff9ab6SJames Feist                                     foundChassis = true;
119083ff9ab6SJames Feist                                     break;
119183ff9ab6SJames Feist                                 }
119283ff9ab6SJames Feist                             }
119383ff9ab6SJames Feist                             if (!foundChassis)
119483ff9ab6SJames Feist                             {
119583ff9ab6SJames Feist                                 BMCWEB_LOG_ERROR
119683ff9ab6SJames Feist                                     << "Failed to find chassis on dbus";
119783ff9ab6SJames Feist                                 messages::resourceMissingAtURI(
119835a62c7cSJason M. Bills                                     response->res,
119935a62c7cSJason M. Bills                                     "/redfish/v1/Chassis/" + chassis);
120083ff9ab6SJames Feist                                 return;
120183ff9ab6SJames Feist                             }
120283ff9ab6SJames Feist 
120383ff9ab6SJames Feist                             crow::connections::systemBus->async_method_call(
120483ff9ab6SJames Feist                                 [response](const boost::system::error_code ec) {
120583ff9ab6SJames Feist                                     if (ec)
120683ff9ab6SJames Feist                                     {
120783ff9ab6SJames Feist                                         BMCWEB_LOG_ERROR
120883ff9ab6SJames Feist                                             << "Error Adding Pid Object " << ec;
120935a62c7cSJason M. Bills                                         messages::internalError(response->res);
1210b6baeaa4SJames Feist                                         return;
121183ff9ab6SJames Feist                                     }
1212b6baeaa4SJames Feist                                     messages::success(response->res);
121383ff9ab6SJames Feist                                 },
121483ff9ab6SJames Feist                                 "xyz.openbmc_project.EntityManager", chassis,
121583ff9ab6SJames Feist                                 "xyz.openbmc_project.AddObject", "AddObject",
121683ff9ab6SJames Feist                                 output);
121783ff9ab6SJames Feist                         }
121883ff9ab6SJames Feist                     }
121983ff9ab6SJames Feist                 }
122083ff9ab6SJames Feist             },
122183ff9ab6SJames Feist             "xyz.openbmc_project.EntityManager", "/", objectManagerIface,
122283ff9ab6SJames Feist             "GetManagedObjects");
122383ff9ab6SJames Feist     }
12245b4aa86bSJames Feist 
12255b4aa86bSJames Feist     void doPatch(crow::Response& res, const crow::Request& req,
12265b4aa86bSJames Feist                  const std::vector<std::string>& params) override
12275b4aa86bSJames Feist     {
12280627a2c7SEd Tanous         std::optional<nlohmann::json> oem;
12290627a2c7SEd Tanous 
12300627a2c7SEd Tanous         if (!json_util::readJson(req, res, "Oem", oem))
123183ff9ab6SJames Feist         {
123283ff9ab6SJames Feist             return;
123383ff9ab6SJames Feist         }
12340627a2c7SEd Tanous 
123583ff9ab6SJames Feist         std::shared_ptr<AsyncResp> response = std::make_shared<AsyncResp>(res);
12360627a2c7SEd Tanous 
12370627a2c7SEd Tanous         if (oem)
123883ff9ab6SJames Feist         {
12395f2caaefSJames Feist             std::optional<nlohmann::json> openbmc;
124043b761d0SEd Tanous             if (!redfish::json_util::readJson(*oem, res, "OpenBmc", openbmc))
124183ff9ab6SJames Feist             {
124243b761d0SEd Tanous                 BMCWEB_LOG_ERROR << "Line:" << __LINE__ << ", Illegal Property "
124343b761d0SEd Tanous                                  << oem->dump();
124483ff9ab6SJames Feist                 return;
124583ff9ab6SJames Feist             }
12465f2caaefSJames Feist             if (openbmc)
124783ff9ab6SJames Feist             {
12485f2caaefSJames Feist                 std::optional<nlohmann::json> fan;
124943b761d0SEd Tanous                 if (!redfish::json_util::readJson(*openbmc, res, "Fan", fan))
125083ff9ab6SJames Feist                 {
12515f2caaefSJames Feist                     BMCWEB_LOG_ERROR << "Line:" << __LINE__
12525f2caaefSJames Feist                                      << ", Illegal Property "
12535f2caaefSJames Feist                                      << openbmc->dump();
125483ff9ab6SJames Feist                     return;
125583ff9ab6SJames Feist                 }
12565f2caaefSJames Feist                 if (fan)
125783ff9ab6SJames Feist                 {
12585f2caaefSJames Feist                     setPidValues(response, *fan);
125983ff9ab6SJames Feist                 }
126083ff9ab6SJames Feist             }
126183ff9ab6SJames Feist         }
126283ff9ab6SJames Feist     }
12639c310685SBorawski.Lukasz 
12641abe55efSEd Tanous     std::string getDateTime() const
12651abe55efSEd Tanous     {
12669c310685SBorawski.Lukasz         std::array<char, 128> dateTime;
12679c310685SBorawski.Lukasz         std::string redfishDateTime("0000-00-00T00:00:00Z00:00");
12689c310685SBorawski.Lukasz         std::time_t time = std::time(nullptr);
12699c310685SBorawski.Lukasz 
12709c310685SBorawski.Lukasz         if (std::strftime(dateTime.begin(), dateTime.size(), "%FT%T%z",
12711abe55efSEd Tanous                           std::localtime(&time)))
12721abe55efSEd Tanous         {
12739c310685SBorawski.Lukasz             // insert the colon required by the ISO 8601 standard
12749c310685SBorawski.Lukasz             redfishDateTime = std::string(dateTime.data());
12759c310685SBorawski.Lukasz             redfishDateTime.insert(redfishDateTime.end() - 2, ':');
12769c310685SBorawski.Lukasz         }
12779c310685SBorawski.Lukasz 
12789c310685SBorawski.Lukasz         return redfishDateTime;
12799c310685SBorawski.Lukasz     }
12800f74e643SEd Tanous 
12810f74e643SEd Tanous     std::string uuid;
12829c310685SBorawski.Lukasz };
12839c310685SBorawski.Lukasz 
12841abe55efSEd Tanous class ManagerCollection : public Node
12851abe55efSEd Tanous {
12869c310685SBorawski.Lukasz   public:
12871abe55efSEd Tanous     ManagerCollection(CrowApp& app) : Node(app, "/redfish/v1/Managers/")
12881abe55efSEd Tanous     {
1289a434f2bdSEd Tanous         entityPrivileges = {
1290a434f2bdSEd Tanous             {boost::beast::http::verb::get, {{"Login"}}},
1291e0d918bcSEd Tanous             {boost::beast::http::verb::head, {{"Login"}}},
1292e0d918bcSEd Tanous             {boost::beast::http::verb::patch, {{"ConfigureManager"}}},
1293e0d918bcSEd Tanous             {boost::beast::http::verb::put, {{"ConfigureManager"}}},
1294e0d918bcSEd Tanous             {boost::beast::http::verb::delete_, {{"ConfigureManager"}}},
1295e0d918bcSEd Tanous             {boost::beast::http::verb::post, {{"ConfigureManager"}}}};
12969c310685SBorawski.Lukasz     }
12979c310685SBorawski.Lukasz 
12989c310685SBorawski.Lukasz   private:
129955c7b7a2SEd Tanous     void doGet(crow::Response& res, const crow::Request& req,
13001abe55efSEd Tanous                const std::vector<std::string>& params) override
13011abe55efSEd Tanous     {
130283ff9ab6SJames Feist         // Collections don't include the static data added by SubRoute
130383ff9ab6SJames Feist         // because it has a duplicate entry for members
130455c7b7a2SEd Tanous         res.jsonValue["@odata.id"] = "/redfish/v1/Managers";
130555c7b7a2SEd Tanous         res.jsonValue["@odata.type"] = "#ManagerCollection.ManagerCollection";
130655c7b7a2SEd Tanous         res.jsonValue["@odata.context"] =
130755c7b7a2SEd Tanous             "/redfish/v1/$metadata#ManagerCollection.ManagerCollection";
130855c7b7a2SEd Tanous         res.jsonValue["Name"] = "Manager Collection";
130955c7b7a2SEd Tanous         res.jsonValue["Members@odata.count"] = 1;
131055c7b7a2SEd Tanous         res.jsonValue["Members"] = {
13115b4aa86bSJames Feist             {{"@odata.id", "/redfish/v1/Managers/bmc"}}};
13129c310685SBorawski.Lukasz         res.end();
13139c310685SBorawski.Lukasz     }
13149c310685SBorawski.Lukasz };
13159c310685SBorawski.Lukasz } // namespace redfish
1316