xref: /openbmc/bmcweb/features/redfish/lib/managers.hpp (revision 43b761d0c8f5c4c39199093ee4bcd60698e77138)
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;
1985b4aa86bSJames Feist                     if (intfPair.first == pidZoneConfigurationIface)
1995b4aa86bSJames Feist                     {
2005b4aa86bSJames Feist                         std::string chassis;
2015b4aa86bSJames Feist                         if (!dbus::utility::getNthStringFromPath(
2025b4aa86bSJames Feist                                 pathPair.first.str, 5, chassis))
2035b4aa86bSJames Feist                         {
2045b4aa86bSJames Feist                             chassis = "#IllegalValue";
2055b4aa86bSJames Feist                         }
2065b4aa86bSJames Feist                         nlohmann::json& zone = zones[name];
2075b4aa86bSJames Feist                         zone["Chassis"] = {
2085b4aa86bSJames Feist                             {"@odata.id", "/redfish/v1/Chassis/" + chassis}};
2095b4aa86bSJames Feist                         zone["@odata.id"] = "/redfish/v1/Managers/bmc#/Oem/"
2105b4aa86bSJames Feist                                             "OpenBmc/Fan/FanZones/" +
2115b4aa86bSJames Feist                                             name;
2125b4aa86bSJames Feist                         zone["@odata.type"] = "#OemManager.FanZone";
2135b4aa86bSJames Feist                         zone["@odata.context"] =
2145b4aa86bSJames Feist                             "/redfish/v1/$metadata#OemManager.FanZone";
215b7a08d04SJames Feist                         config = &zone;
2165b4aa86bSJames Feist                     }
2175b4aa86bSJames Feist 
218b7a08d04SJames Feist                     else if (intfPair.first == stepwiseConfigurationIface)
2195b4aa86bSJames Feist                     {
220b7a08d04SJames Feist                         nlohmann::json& controller = stepwise[name];
221b7a08d04SJames Feist                         config = &controller;
2225b4aa86bSJames Feist 
223b7a08d04SJames Feist                         controller["@odata.id"] =
224b7a08d04SJames Feist                             "/redfish/v1/Managers/bmc#/Oem/"
225b7a08d04SJames Feist                             "OpenBmc/Fan/StepwiseControllers/" +
226b7a08d04SJames Feist                             std::string(name);
227b7a08d04SJames Feist                         controller["@odata.type"] =
228b7a08d04SJames Feist                             "#OemManager.StepwiseController";
229b7a08d04SJames Feist 
230b7a08d04SJames Feist                         controller["@odata.context"] =
231b7a08d04SJames Feist                             "/redfish/v1/"
232b7a08d04SJames Feist                             "$metadata#OemManager.StepwiseController";
2335b4aa86bSJames Feist                     }
2345b4aa86bSJames Feist 
2355b4aa86bSJames Feist                     // pid and fans are off the same configuration
236b7a08d04SJames Feist                     else if (intfPair.first == pidConfigurationIface)
2375b4aa86bSJames Feist                     {
2385b4aa86bSJames Feist                         const std::string* classPtr = nullptr;
2395b4aa86bSJames Feist                         auto findClass = intfPair.second.find("Class");
2405b4aa86bSJames Feist                         if (findClass != intfPair.second.end())
2415b4aa86bSJames Feist                         {
242abf2add6SEd Tanous                             classPtr =
243abf2add6SEd Tanous                                 std::get_if<std::string>(&findClass->second);
2445b4aa86bSJames Feist                         }
2455b4aa86bSJames Feist                         if (classPtr == nullptr)
2465b4aa86bSJames Feist                         {
2475b4aa86bSJames Feist                             BMCWEB_LOG_ERROR << "Pid Class Field illegal";
248a08b46ccSJason M. Bills                             messages::internalError(asyncResp->res);
2495b4aa86bSJames Feist                             return;
2505b4aa86bSJames Feist                         }
2515b4aa86bSJames Feist                         bool isFan = *classPtr == "fan";
2525b4aa86bSJames Feist                         nlohmann::json& element =
2535b4aa86bSJames Feist                             isFan ? fans[name] : pids[name];
254b7a08d04SJames Feist                         config = &element;
2555b4aa86bSJames Feist                         if (isFan)
2565b4aa86bSJames Feist                         {
2575b4aa86bSJames Feist                             element["@odata.id"] =
2585b4aa86bSJames Feist                                 "/redfish/v1/Managers/bmc#/Oem/"
2595b4aa86bSJames Feist                                 "OpenBmc/Fan/FanControllers/" +
2605b4aa86bSJames Feist                                 std::string(name);
2615b4aa86bSJames Feist                             element["@odata.type"] =
2625b4aa86bSJames Feist                                 "#OemManager.FanController";
2635b4aa86bSJames Feist 
2645b4aa86bSJames Feist                             element["@odata.context"] =
2655b4aa86bSJames Feist                                 "/redfish/v1/"
2665b4aa86bSJames Feist                                 "$metadata#OemManager.FanController";
2675b4aa86bSJames Feist                         }
2685b4aa86bSJames Feist                         else
2695b4aa86bSJames Feist                         {
2705b4aa86bSJames Feist                             element["@odata.id"] =
2715b4aa86bSJames Feist                                 "/redfish/v1/Managers/bmc#/Oem/"
2725b4aa86bSJames Feist                                 "OpenBmc/Fan/PidControllers/" +
2735b4aa86bSJames Feist                                 std::string(name);
2745b4aa86bSJames Feist                             element["@odata.type"] =
2755b4aa86bSJames Feist                                 "#OemManager.PidController";
2765b4aa86bSJames Feist                             element["@odata.context"] =
2775b4aa86bSJames Feist                                 "/redfish/v1/$metadata"
2785b4aa86bSJames Feist                                 "#OemManager.PidController";
2795b4aa86bSJames Feist                         }
280b7a08d04SJames Feist                     }
281b7a08d04SJames Feist                     else
282b7a08d04SJames Feist                     {
283b7a08d04SJames Feist                         BMCWEB_LOG_ERROR << "Unexpected configuration";
284b7a08d04SJames Feist                         messages::internalError(asyncResp->res);
285b7a08d04SJames Feist                         return;
286b7a08d04SJames Feist                     }
287b7a08d04SJames Feist 
288b7a08d04SJames Feist                     // used for making maps out of 2 vectors
289b7a08d04SJames Feist                     const std::vector<double>* keys = nullptr;
290b7a08d04SJames Feist                     const std::vector<double>* values = nullptr;
291b7a08d04SJames Feist 
292b7a08d04SJames Feist                     for (const auto& propertyPair : intfPair.second)
293b7a08d04SJames Feist                     {
294b7a08d04SJames Feist                         if (propertyPair.first == "Type" ||
295b7a08d04SJames Feist                             propertyPair.first == "Class" ||
296b7a08d04SJames Feist                             propertyPair.first == "Name")
297b7a08d04SJames Feist                         {
298b7a08d04SJames Feist                             continue;
299b7a08d04SJames Feist                         }
300b7a08d04SJames Feist 
301b7a08d04SJames Feist                         // zones
302b7a08d04SJames Feist                         if (intfPair.first == pidZoneConfigurationIface)
303b7a08d04SJames Feist                         {
304b7a08d04SJames Feist                             const double* ptr =
305abf2add6SEd Tanous                                 std::get_if<double>(&propertyPair.second);
306b7a08d04SJames Feist                             if (ptr == nullptr)
307b7a08d04SJames Feist                             {
308b7a08d04SJames Feist                                 BMCWEB_LOG_ERROR << "Field Illegal "
309b7a08d04SJames Feist                                                  << propertyPair.first;
310b7a08d04SJames Feist                                 messages::internalError(asyncResp->res);
311b7a08d04SJames Feist                                 return;
312b7a08d04SJames Feist                             }
313b7a08d04SJames Feist                             (*config)[propertyPair.first] = *ptr;
314b7a08d04SJames Feist                         }
315b7a08d04SJames Feist 
316b7a08d04SJames Feist                         if (intfPair.first == stepwiseConfigurationIface)
317b7a08d04SJames Feist                         {
318b7a08d04SJames Feist                             if (propertyPair.first == "Reading" ||
319b7a08d04SJames Feist                                 propertyPair.first == "Output")
320b7a08d04SJames Feist                             {
321b7a08d04SJames Feist                                 const std::vector<double>* ptr =
322abf2add6SEd Tanous                                     std::get_if<std::vector<double>>(
323b7a08d04SJames Feist                                         &propertyPair.second);
324b7a08d04SJames Feist 
325b7a08d04SJames Feist                                 if (ptr == nullptr)
326b7a08d04SJames Feist                                 {
327b7a08d04SJames Feist                                     BMCWEB_LOG_ERROR << "Field Illegal "
328b7a08d04SJames Feist                                                      << propertyPair.first;
329b7a08d04SJames Feist                                     messages::internalError(asyncResp->res);
330b7a08d04SJames Feist                                     return;
331b7a08d04SJames Feist                                 }
332b7a08d04SJames Feist 
333b7a08d04SJames Feist                                 if (propertyPair.first == "Reading")
334b7a08d04SJames Feist                                 {
335b7a08d04SJames Feist                                     keys = ptr;
336b7a08d04SJames Feist                                 }
337b7a08d04SJames Feist                                 else
338b7a08d04SJames Feist                                 {
339b7a08d04SJames Feist                                     values = ptr;
340b7a08d04SJames Feist                                 }
341b7a08d04SJames Feist                                 if (keys && values)
342b7a08d04SJames Feist                                 {
343b7a08d04SJames Feist                                     if (keys->size() != values->size())
344b7a08d04SJames Feist                                     {
345b7a08d04SJames Feist                                         BMCWEB_LOG_ERROR
346b7a08d04SJames Feist                                             << "Reading and Output size don't "
347b7a08d04SJames Feist                                                "match ";
348b7a08d04SJames Feist                                         messages::internalError(asyncResp->res);
349b7a08d04SJames Feist                                         return;
350b7a08d04SJames Feist                                     }
351b7a08d04SJames Feist                                     nlohmann::json& steps = (*config)["Steps"];
352b7a08d04SJames Feist                                     steps = nlohmann::json::array();
353b7a08d04SJames Feist                                     for (size_t ii = 0; ii < keys->size(); ii++)
354b7a08d04SJames Feist                                     {
355b7a08d04SJames Feist                                         steps.push_back(
356b7a08d04SJames Feist                                             {{"Target", (*keys)[ii]},
357b7a08d04SJames Feist                                              {"Output", (*values)[ii]}});
358b7a08d04SJames Feist                                     }
359b7a08d04SJames Feist                                 }
360b7a08d04SJames Feist                             }
361b7a08d04SJames Feist                             if (propertyPair.first == "NegativeHysteresis" ||
362b7a08d04SJames Feist                                 propertyPair.first == "PositiveHysteresis")
363b7a08d04SJames Feist                             {
364b7a08d04SJames Feist                                 const double* ptr =
365abf2add6SEd Tanous                                     std::get_if<double>(&propertyPair.second);
366b7a08d04SJames Feist                                 if (ptr == nullptr)
367b7a08d04SJames Feist                                 {
368b7a08d04SJames Feist                                     BMCWEB_LOG_ERROR << "Field Illegal "
369b7a08d04SJames Feist                                                      << propertyPair.first;
370b7a08d04SJames Feist                                     messages::internalError(asyncResp->res);
371b7a08d04SJames Feist                                     return;
372b7a08d04SJames Feist                                 }
373b7a08d04SJames Feist                                 (*config)[propertyPair.first] = *ptr;
374b7a08d04SJames Feist                             }
375b7a08d04SJames Feist                         }
376b7a08d04SJames Feist 
377b7a08d04SJames Feist                         // pid and fans are off the same configuration
378b7a08d04SJames Feist                         if (intfPair.first == pidConfigurationIface ||
379b7a08d04SJames Feist                             intfPair.first == stepwiseConfigurationIface)
380b7a08d04SJames Feist                         {
3815b4aa86bSJames Feist 
3825b4aa86bSJames Feist                             if (propertyPair.first == "Zones")
3835b4aa86bSJames Feist                             {
3845b4aa86bSJames Feist                                 const std::vector<std::string>* inputs =
385abf2add6SEd Tanous                                     std::get_if<std::vector<std::string>>(
3861b6b96c5SEd Tanous                                         &propertyPair.second);
3875b4aa86bSJames Feist 
3885b4aa86bSJames Feist                                 if (inputs == nullptr)
3895b4aa86bSJames Feist                                 {
3905b4aa86bSJames Feist                                     BMCWEB_LOG_ERROR
3915b4aa86bSJames Feist                                         << "Zones Pid Field Illegal";
392a08b46ccSJason M. Bills                                     messages::internalError(asyncResp->res);
3935b4aa86bSJames Feist                                     return;
3945b4aa86bSJames Feist                                 }
395b7a08d04SJames Feist                                 auto& data = (*config)[propertyPair.first];
3965b4aa86bSJames Feist                                 data = nlohmann::json::array();
3975b4aa86bSJames Feist                                 for (std::string itemCopy : *inputs)
3985b4aa86bSJames Feist                                 {
3995b4aa86bSJames Feist                                     dbus::utility::escapePathForDbus(itemCopy);
4005b4aa86bSJames Feist                                     data.push_back(
4015b4aa86bSJames Feist                                         {{"@odata.id",
4025b4aa86bSJames Feist                                           "/redfish/v1/Managers/bmc#/Oem/"
4035b4aa86bSJames Feist                                           "OpenBmc/Fan/FanZones/" +
4045b4aa86bSJames Feist                                               itemCopy}});
4055b4aa86bSJames Feist                                 }
4065b4aa86bSJames Feist                             }
4075b4aa86bSJames Feist                             // todo(james): may never happen, but this
4085b4aa86bSJames Feist                             // assumes configuration data referenced in the
4095b4aa86bSJames Feist                             // PID config is provided by the same daemon, we
4105b4aa86bSJames Feist                             // could add another loop to cover all cases,
4115b4aa86bSJames Feist                             // but I'm okay kicking this can down the road a
4125b4aa86bSJames Feist                             // bit
4135b4aa86bSJames Feist 
4145b4aa86bSJames Feist                             else if (propertyPair.first == "Inputs" ||
4155b4aa86bSJames Feist                                      propertyPair.first == "Outputs")
4165b4aa86bSJames Feist                             {
417b7a08d04SJames Feist                                 auto& data = (*config)[propertyPair.first];
4185b4aa86bSJames Feist                                 const std::vector<std::string>* inputs =
419abf2add6SEd Tanous                                     std::get_if<std::vector<std::string>>(
4201b6b96c5SEd Tanous                                         &propertyPair.second);
4215b4aa86bSJames Feist 
4225b4aa86bSJames Feist                                 if (inputs == nullptr)
4235b4aa86bSJames Feist                                 {
4245b4aa86bSJames Feist                                     BMCWEB_LOG_ERROR << "Field Illegal "
4255b4aa86bSJames Feist                                                      << propertyPair.first;
426f12894f8SJason M. Bills                                     messages::internalError(asyncResp->res);
4275b4aa86bSJames Feist                                     return;
4285b4aa86bSJames Feist                                 }
4295b4aa86bSJames Feist                                 data = *inputs;
4305b4aa86bSJames Feist                             } // doubles
4315b4aa86bSJames Feist                             else if (propertyPair.first ==
4325b4aa86bSJames Feist                                          "FFGainCoefficient" ||
4335b4aa86bSJames Feist                                      propertyPair.first == "FFOffCoefficient" ||
4345b4aa86bSJames Feist                                      propertyPair.first == "ICoefficient" ||
4355b4aa86bSJames Feist                                      propertyPair.first == "ILimitMax" ||
4365b4aa86bSJames Feist                                      propertyPair.first == "ILimitMin" ||
437aad1a257SJames Feist                                      propertyPair.first ==
438aad1a257SJames Feist                                          "PositiveHysteresis" ||
439aad1a257SJames Feist                                      propertyPair.first ==
440aad1a257SJames Feist                                          "NegativeHysteresis" ||
4415b4aa86bSJames Feist                                      propertyPair.first == "OutLimitMax" ||
4425b4aa86bSJames Feist                                      propertyPair.first == "OutLimitMin" ||
4435b4aa86bSJames Feist                                      propertyPair.first == "PCoefficient" ||
4447625cb81SJames Feist                                      propertyPair.first == "SetPoint" ||
4455b4aa86bSJames Feist                                      propertyPair.first == "SlewNeg" ||
4465b4aa86bSJames Feist                                      propertyPair.first == "SlewPos")
4475b4aa86bSJames Feist                             {
4485b4aa86bSJames Feist                                 const double* ptr =
449abf2add6SEd Tanous                                     std::get_if<double>(&propertyPair.second);
4505b4aa86bSJames Feist                                 if (ptr == nullptr)
4515b4aa86bSJames Feist                                 {
4525b4aa86bSJames Feist                                     BMCWEB_LOG_ERROR << "Field Illegal "
4535b4aa86bSJames Feist                                                      << propertyPair.first;
454f12894f8SJason M. Bills                                     messages::internalError(asyncResp->res);
4555b4aa86bSJames Feist                                     return;
4565b4aa86bSJames Feist                                 }
457b7a08d04SJames Feist                                 (*config)[propertyPair.first] = *ptr;
4585b4aa86bSJames Feist                             }
4595b4aa86bSJames Feist                         }
4605b4aa86bSJames Feist                     }
4615b4aa86bSJames Feist                 }
4625b4aa86bSJames Feist             }
4635b4aa86bSJames Feist         },
4645b4aa86bSJames Feist         connection, path, objectManagerIface, "GetManagedObjects");
4655b4aa86bSJames Feist }
466ca537928SJennifer Lee 
46783ff9ab6SJames Feist enum class CreatePIDRet
46883ff9ab6SJames Feist {
46983ff9ab6SJames Feist     fail,
47083ff9ab6SJames Feist     del,
47183ff9ab6SJames Feist     patch
47283ff9ab6SJames Feist };
47383ff9ab6SJames Feist 
4745f2caaefSJames Feist static bool getZonesFromJsonReq(const std::shared_ptr<AsyncResp>& response,
4755f2caaefSJames Feist                                 std::vector<nlohmann::json>& config,
4765f2caaefSJames Feist                                 std::vector<std::string>& zones)
4775f2caaefSJames Feist {
4785f2caaefSJames Feist 
4795f2caaefSJames Feist     for (auto& odata : config)
4805f2caaefSJames Feist     {
4815f2caaefSJames Feist         std::string path;
4825f2caaefSJames Feist         if (!redfish::json_util::readJson(odata, response->res, "@odata.id",
4835f2caaefSJames Feist                                           path))
4845f2caaefSJames Feist         {
4855f2caaefSJames Feist             return false;
4865f2caaefSJames Feist         }
4875f2caaefSJames Feist         std::string input;
4885f2caaefSJames Feist         if (!dbus::utility::getNthStringFromPath(path, 4, input))
4895f2caaefSJames Feist         {
4905f2caaefSJames Feist             BMCWEB_LOG_ERROR << "Got invalid path " << path;
4915f2caaefSJames Feist             BMCWEB_LOG_ERROR << "Illegal Type Zones";
4925f2caaefSJames Feist             messages::propertyValueFormatError(response->res, odata.dump(),
4935f2caaefSJames Feist                                                "Zones");
4945f2caaefSJames Feist             return false;
4955f2caaefSJames Feist         }
4965f2caaefSJames Feist         boost::replace_all(input, "_", " ");
4975f2caaefSJames Feist         zones.emplace_back(std::move(input));
4985f2caaefSJames Feist     }
4995f2caaefSJames Feist     return true;
5005f2caaefSJames Feist }
5015f2caaefSJames Feist 
50283ff9ab6SJames Feist static CreatePIDRet createPidInterface(
50383ff9ab6SJames Feist     const std::shared_ptr<AsyncResp>& response, const std::string& type,
5045f2caaefSJames Feist     nlohmann::json&& record, const std::string& path,
50583ff9ab6SJames Feist     const dbus::utility::ManagedObjectType& managedObj, bool createNewObject,
50683ff9ab6SJames Feist     boost::container::flat_map<std::string, dbus::utility::DbusVariantType>&
50783ff9ab6SJames Feist         output,
50883ff9ab6SJames Feist     std::string& chassis)
50983ff9ab6SJames Feist {
51083ff9ab6SJames Feist 
5115f2caaefSJames Feist     // common deleter
5125f2caaefSJames Feist     if (record == nullptr)
5135f2caaefSJames Feist     {
5145f2caaefSJames Feist         std::string iface;
5155f2caaefSJames Feist         if (type == "PidControllers" || type == "FanControllers")
5165f2caaefSJames Feist         {
5175f2caaefSJames Feist             iface = pidConfigurationIface;
5185f2caaefSJames Feist         }
5195f2caaefSJames Feist         else if (type == "FanZones")
5205f2caaefSJames Feist         {
5215f2caaefSJames Feist             iface = pidZoneConfigurationIface;
5225f2caaefSJames Feist         }
5235f2caaefSJames Feist         else if (type == "StepwiseControllers")
5245f2caaefSJames Feist         {
5255f2caaefSJames Feist             iface = stepwiseConfigurationIface;
5265f2caaefSJames Feist         }
5275f2caaefSJames Feist         else
5285f2caaefSJames Feist         {
5295f2caaefSJames Feist             BMCWEB_LOG_ERROR << "Line:" << __LINE__ << ", Illegal Type "
5305f2caaefSJames Feist                              << type;
5315f2caaefSJames Feist             messages::propertyUnknown(response->res, type);
5325f2caaefSJames Feist             return CreatePIDRet::fail;
5335f2caaefSJames Feist         }
5345f2caaefSJames Feist         // delete interface
5355f2caaefSJames Feist         crow::connections::systemBus->async_method_call(
5365f2caaefSJames Feist             [response, path](const boost::system::error_code ec) {
5375f2caaefSJames Feist                 if (ec)
5385f2caaefSJames Feist                 {
5395f2caaefSJames Feist                     BMCWEB_LOG_ERROR << "Error patching " << path << ": " << ec;
5405f2caaefSJames Feist                     messages::internalError(response->res);
5415f2caaefSJames Feist                 }
5425f2caaefSJames Feist             },
5435f2caaefSJames Feist             "xyz.openbmc_project.EntityManager", path, iface, "Delete");
5445f2caaefSJames Feist         return CreatePIDRet::del;
5455f2caaefSJames Feist     }
5465f2caaefSJames Feist 
54783ff9ab6SJames Feist     if (type == "PidControllers" || type == "FanControllers")
54883ff9ab6SJames Feist     {
54983ff9ab6SJames Feist         if (createNewObject)
55083ff9ab6SJames Feist         {
55183ff9ab6SJames Feist             output["Class"] = type == "PidControllers" ? std::string("temp")
55283ff9ab6SJames Feist                                                        : std::string("fan");
55383ff9ab6SJames Feist             output["Type"] = std::string("Pid");
55483ff9ab6SJames Feist         }
5555f2caaefSJames Feist 
5565f2caaefSJames Feist         std::optional<std::vector<nlohmann::json>> zones;
5575f2caaefSJames Feist         std::optional<std::vector<std::string>> inputs;
5585f2caaefSJames Feist         std::optional<std::vector<std::string>> outputs;
5595f2caaefSJames Feist         std::map<std::string, std::optional<double>> doubles;
5605f2caaefSJames Feist         if (!redfish::json_util::readJson(
5615f2caaefSJames Feist                 record, response->res, "Inputs", inputs, "Outputs", outputs,
5625f2caaefSJames Feist                 "Zones", zones, "FFGainCoefficient",
5635f2caaefSJames Feist                 doubles["FFGainCoefficient"], "FFOffCoefficient",
5645f2caaefSJames Feist                 doubles["FFOffCoefficient"], "ICoefficient",
5655f2caaefSJames Feist                 doubles["ICoefficient"], "ILimitMax", doubles["ILimitMax"],
5665f2caaefSJames Feist                 "ILimitMin", doubles["ILimitMin"], "OutLimitMax",
5675f2caaefSJames Feist                 doubles["OutLimitMax"], "OutLimitMin", doubles["OutLimitMin"],
5685f2caaefSJames Feist                 "PCoefficient", doubles["PCoefficient"], "SetPoint",
5695f2caaefSJames Feist                 doubles["SetPoint"], "SlewNeg", doubles["SlewNeg"], "SlewPos",
570aad1a257SJames Feist                 doubles["SlewPos"], "PositiveHysteresis",
571aad1a257SJames Feist                 doubles["PositiveHysteresis"], "NegativeHysteresis",
572aad1a257SJames Feist                 doubles["NegativeHysteresis"]))
57383ff9ab6SJames Feist         {
5745f2caaefSJames Feist             BMCWEB_LOG_ERROR << "Line:" << __LINE__ << ", Illegal Property "
5755f2caaefSJames Feist                              << record.dump();
5765f2caaefSJames Feist             return CreatePIDRet::fail;
57783ff9ab6SJames Feist         }
5785f2caaefSJames Feist         if (zones)
5795f2caaefSJames Feist         {
5805f2caaefSJames Feist             std::vector<std::string> zonesStr;
5815f2caaefSJames Feist             if (!getZonesFromJsonReq(response, *zones, zonesStr))
5825f2caaefSJames Feist             {
5835f2caaefSJames Feist                 BMCWEB_LOG_ERROR << "Line:" << __LINE__ << ", Illegal Zones";
5845f2caaefSJames Feist                 return CreatePIDRet::fail;
5855f2caaefSJames Feist             }
5865f2caaefSJames Feist             output["Zones"] = std::move(zonesStr);
5875f2caaefSJames Feist         }
5885f2caaefSJames Feist         if (inputs || outputs)
5895f2caaefSJames Feist         {
5905f2caaefSJames Feist             std::array<std::optional<std::vector<std::string>>*, 2> containers =
5915f2caaefSJames Feist                 {&inputs, &outputs};
5925f2caaefSJames Feist             size_t index = 0;
5935f2caaefSJames Feist             for (const auto& containerPtr : containers)
5945f2caaefSJames Feist             {
5955f2caaefSJames Feist                 std::optional<std::vector<std::string>>& container =
5965f2caaefSJames Feist                     *containerPtr;
5975f2caaefSJames Feist                 if (!container)
5985f2caaefSJames Feist                 {
5995f2caaefSJames Feist                     index++;
6005f2caaefSJames Feist                     continue;
60183ff9ab6SJames Feist                 }
60283ff9ab6SJames Feist 
6035f2caaefSJames Feist                 for (std::string& value : *container)
60483ff9ab6SJames Feist                 {
60583ff9ab6SJames Feist 
60683ff9ab6SJames Feist                     // try to find the sensor in the
60783ff9ab6SJames Feist                     // configuration
60883ff9ab6SJames Feist                     if (chassis.empty())
60983ff9ab6SJames Feist                     {
6105f2caaefSJames Feist                         std::string escaped =
6115f2caaefSJames Feist                             boost::replace_all_copy(value, " ", "_");
61283ff9ab6SJames Feist                         std::find_if(
61383ff9ab6SJames Feist                             managedObj.begin(), managedObj.end(),
6145f2caaefSJames Feist                             [&chassis, &escaped](const auto& obj) {
61583ff9ab6SJames Feist                                 if (boost::algorithm::ends_with(obj.first.str,
6165f2caaefSJames Feist                                                                 escaped))
61783ff9ab6SJames Feist                                 {
61883ff9ab6SJames Feist                                     return dbus::utility::getNthStringFromPath(
61983ff9ab6SJames Feist                                         obj.first.str, 5, chassis);
62083ff9ab6SJames Feist                                 }
62183ff9ab6SJames Feist                                 return false;
62283ff9ab6SJames Feist                             });
62383ff9ab6SJames Feist                     }
6245f2caaefSJames Feist                     boost::replace_all(value, "_", " ");
62583ff9ab6SJames Feist                 }
6265f2caaefSJames Feist                 std::string key;
6275f2caaefSJames Feist                 if (index == 0)
6285f2caaefSJames Feist                 {
6295f2caaefSJames Feist                     key = "Inputs";
6305f2caaefSJames Feist                 }
6315f2caaefSJames Feist                 else
6325f2caaefSJames Feist                 {
6335f2caaefSJames Feist                     key = "Outputs";
6345f2caaefSJames Feist                 }
6355f2caaefSJames Feist                 output[key] = *container;
6365f2caaefSJames Feist                 index++;
6375f2caaefSJames Feist             }
63883ff9ab6SJames Feist         }
63983ff9ab6SJames Feist 
64083ff9ab6SJames Feist         // doubles
6415f2caaefSJames Feist         for (const auto& pairs : doubles)
64283ff9ab6SJames Feist         {
6435f2caaefSJames Feist             if (!pairs.second)
64483ff9ab6SJames Feist             {
6455f2caaefSJames Feist                 continue;
64683ff9ab6SJames Feist             }
6475f2caaefSJames Feist             BMCWEB_LOG_DEBUG << pairs.first << " = " << *pairs.second;
6485f2caaefSJames Feist             output[pairs.first] = *(pairs.second);
6495f2caaefSJames Feist         }
65083ff9ab6SJames Feist     }
65183ff9ab6SJames Feist 
65283ff9ab6SJames Feist     else if (type == "FanZones")
65383ff9ab6SJames Feist     {
65483ff9ab6SJames Feist         output["Type"] = std::string("Pid.Zone");
65583ff9ab6SJames Feist 
6565f2caaefSJames Feist         std::optional<nlohmann::json> chassisContainer;
6575f2caaefSJames Feist         std::optional<double> failSafePercent;
6585f2caaefSJames Feist         std::optional<double> minThermalRpm;
6595f2caaefSJames Feist         if (!redfish::json_util::readJson(record, response->res, "Chassis",
6605f2caaefSJames Feist                                           chassisContainer, "FailSafePercent",
6615f2caaefSJames Feist                                           failSafePercent, "MinThermalRpm",
6625f2caaefSJames Feist                                           minThermalRpm))
66383ff9ab6SJames Feist         {
6645f2caaefSJames Feist             BMCWEB_LOG_ERROR << "Line:" << __LINE__ << ", Illegal Property "
6655f2caaefSJames Feist                              << record.dump();
66683ff9ab6SJames Feist             return CreatePIDRet::fail;
66783ff9ab6SJames Feist         }
6685f2caaefSJames Feist 
6695f2caaefSJames Feist         if (chassisContainer)
67083ff9ab6SJames Feist         {
6715f2caaefSJames Feist 
6725f2caaefSJames Feist             std::string chassisId;
6735f2caaefSJames Feist             if (!redfish::json_util::readJson(*chassisContainer, response->res,
6745f2caaefSJames Feist                                               "@odata.id", chassisId))
6755f2caaefSJames Feist             {
6765f2caaefSJames Feist                 BMCWEB_LOG_ERROR << "Line:" << __LINE__ << ", Illegal Property "
6775f2caaefSJames Feist                                  << chassisContainer->dump();
67883ff9ab6SJames Feist                 return CreatePIDRet::fail;
67983ff9ab6SJames Feist             }
68083ff9ab6SJames Feist 
68183ff9ab6SJames Feist             // /refish/v1/chassis/chassis_name/
6825f2caaefSJames Feist             if (!dbus::utility::getNthStringFromPath(chassisId, 3, chassis))
68383ff9ab6SJames Feist             {
6845f2caaefSJames Feist                 BMCWEB_LOG_ERROR << "Got invalid path " << chassisId;
6855f2caaefSJames Feist                 messages::invalidObject(response->res, chassisId);
68683ff9ab6SJames Feist                 return CreatePIDRet::fail;
68783ff9ab6SJames Feist             }
68883ff9ab6SJames Feist         }
6895f2caaefSJames Feist         if (minThermalRpm)
69083ff9ab6SJames Feist         {
6915f2caaefSJames Feist             output["MinThermalRpm"] = *minThermalRpm;
6925f2caaefSJames Feist         }
6935f2caaefSJames Feist         if (failSafePercent)
69483ff9ab6SJames Feist         {
6955f2caaefSJames Feist             output["FailSafePercent"] = *failSafePercent;
6965f2caaefSJames Feist         }
6975f2caaefSJames Feist     }
6985f2caaefSJames Feist     else if (type == "StepwiseControllers")
6995f2caaefSJames Feist     {
7005f2caaefSJames Feist         output["Type"] = std::string("Stepwise");
7015f2caaefSJames Feist 
7025f2caaefSJames Feist         std::optional<std::vector<nlohmann::json>> zones;
7035f2caaefSJames Feist         std::optional<std::vector<nlohmann::json>> steps;
7045f2caaefSJames Feist         std::optional<std::vector<std::string>> inputs;
7055f2caaefSJames Feist         std::optional<double> positiveHysteresis;
7065f2caaefSJames Feist         std::optional<double> negativeHysteresis;
7075f2caaefSJames Feist         if (!redfish::json_util::readJson(
7085f2caaefSJames Feist                 record, response->res, "Zones", zones, "Steps", steps, "Inputs",
7095f2caaefSJames Feist                 inputs, "PositiveHysteresis", positiveHysteresis,
7105f2caaefSJames Feist                 "NegativeHysteresis", negativeHysteresis))
7115f2caaefSJames Feist         {
7125f2caaefSJames Feist             BMCWEB_LOG_ERROR << "Line:" << __LINE__ << ", Illegal Property "
7135f2caaefSJames Feist                              << record.dump();
71483ff9ab6SJames Feist             return CreatePIDRet::fail;
71583ff9ab6SJames Feist         }
7165f2caaefSJames Feist 
7175f2caaefSJames Feist         if (zones)
71883ff9ab6SJames Feist         {
7195f2caaefSJames Feist             std::vector<std::string> zoneStrs;
7205f2caaefSJames Feist             if (!getZonesFromJsonReq(response, *zones, zoneStrs))
7215f2caaefSJames Feist             {
7225f2caaefSJames Feist                 BMCWEB_LOG_ERROR << "Line:" << __LINE__ << ", Illegal Zones";
72383ff9ab6SJames Feist                 return CreatePIDRet::fail;
72483ff9ab6SJames Feist             }
7255f2caaefSJames Feist             output["Zones"] = std::move(zoneStrs);
7265f2caaefSJames Feist         }
7275f2caaefSJames Feist         if (steps)
7285f2caaefSJames Feist         {
7295f2caaefSJames Feist             std::vector<double> readings;
7305f2caaefSJames Feist             std::vector<double> outputs;
7315f2caaefSJames Feist             for (auto& step : *steps)
7325f2caaefSJames Feist             {
7335f2caaefSJames Feist                 double target;
7345f2caaefSJames Feist                 double output;
7355f2caaefSJames Feist 
7365f2caaefSJames Feist                 if (!redfish::json_util::readJson(step, response->res, "Target",
7375f2caaefSJames Feist                                                   target, "Output", output))
7385f2caaefSJames Feist                 {
7395f2caaefSJames Feist                     BMCWEB_LOG_ERROR << "Line:" << __LINE__
7405f2caaefSJames Feist                                      << ", Illegal Property " << record.dump();
7415f2caaefSJames Feist                     return CreatePIDRet::fail;
7425f2caaefSJames Feist                 }
7435f2caaefSJames Feist                 readings.emplace_back(target);
7445f2caaefSJames Feist                 outputs.emplace_back(output);
7455f2caaefSJames Feist             }
7465f2caaefSJames Feist             output["Reading"] = std::move(readings);
7475f2caaefSJames Feist             output["Output"] = std::move(outputs);
7485f2caaefSJames Feist         }
7495f2caaefSJames Feist         if (inputs)
7505f2caaefSJames Feist         {
7515f2caaefSJames Feist             for (std::string& value : *inputs)
7525f2caaefSJames Feist             {
7535f2caaefSJames Feist                 if (chassis.empty())
7545f2caaefSJames Feist                 {
7555f2caaefSJames Feist                     std::string escaped =
7565f2caaefSJames Feist                         boost::replace_all_copy(value, " ", "_");
7575f2caaefSJames Feist                     std::find_if(
7585f2caaefSJames Feist                         managedObj.begin(), managedObj.end(),
7595f2caaefSJames Feist                         [&chassis, &escaped](const auto& obj) {
7605f2caaefSJames Feist                             if (boost::algorithm::ends_with(obj.first.str,
7615f2caaefSJames Feist                                                             escaped))
7625f2caaefSJames Feist                             {
7635f2caaefSJames Feist                                 return dbus::utility::getNthStringFromPath(
7645f2caaefSJames Feist                                     obj.first.str, 5, chassis);
7655f2caaefSJames Feist                             }
7665f2caaefSJames Feist                             return false;
7675f2caaefSJames Feist                         });
7685f2caaefSJames Feist                 }
7695f2caaefSJames Feist                 boost::replace_all(value, "_", " ");
7705f2caaefSJames Feist             }
7715f2caaefSJames Feist             output["Inputs"] = std::move(*inputs);
7725f2caaefSJames Feist         }
7735f2caaefSJames Feist         if (negativeHysteresis)
7745f2caaefSJames Feist         {
7755f2caaefSJames Feist             output["NegativeHysteresis"] = *negativeHysteresis;
7765f2caaefSJames Feist         }
7775f2caaefSJames Feist         if (positiveHysteresis)
7785f2caaefSJames Feist         {
7795f2caaefSJames Feist             output["PositiveHysteresis"] = *positiveHysteresis;
78083ff9ab6SJames Feist         }
78183ff9ab6SJames Feist     }
78283ff9ab6SJames Feist     else
78383ff9ab6SJames Feist     {
7845f2caaefSJames Feist         BMCWEB_LOG_ERROR << "Line:" << __LINE__ << ", Illegal Type " << type;
78535a62c7cSJason M. Bills         messages::propertyUnknown(response->res, type);
78683ff9ab6SJames Feist         return CreatePIDRet::fail;
78783ff9ab6SJames Feist     }
78883ff9ab6SJames Feist     return CreatePIDRet::patch;
78983ff9ab6SJames Feist }
79083ff9ab6SJames Feist 
7911abe55efSEd Tanous class Manager : public Node
7921abe55efSEd Tanous {
7939c310685SBorawski.Lukasz   public:
7945b4aa86bSJames Feist     Manager(CrowApp& app) : Node(app, "/redfish/v1/Managers/bmc/")
7951abe55efSEd Tanous     {
7960f74e643SEd Tanous         uuid = app.template getMiddleware<crow::persistent_data::Middleware>()
79755c7b7a2SEd Tanous                    .systemUuid;
798a434f2bdSEd Tanous         entityPrivileges = {
799a434f2bdSEd Tanous             {boost::beast::http::verb::get, {{"Login"}}},
800e0d918bcSEd Tanous             {boost::beast::http::verb::head, {{"Login"}}},
801e0d918bcSEd Tanous             {boost::beast::http::verb::patch, {{"ConfigureManager"}}},
802e0d918bcSEd Tanous             {boost::beast::http::verb::put, {{"ConfigureManager"}}},
803e0d918bcSEd Tanous             {boost::beast::http::verb::delete_, {{"ConfigureManager"}}},
804e0d918bcSEd Tanous             {boost::beast::http::verb::post, {{"ConfigureManager"}}}};
8059c310685SBorawski.Lukasz     }
8069c310685SBorawski.Lukasz 
8079c310685SBorawski.Lukasz   private:
8085b4aa86bSJames Feist     void getPidValues(std::shared_ptr<AsyncResp> asyncResp)
8095b4aa86bSJames Feist     {
8105b4aa86bSJames Feist         crow::connections::systemBus->async_method_call(
8115b4aa86bSJames Feist             [asyncResp](const boost::system::error_code ec,
8125b4aa86bSJames Feist                         const crow::openbmc_mapper::GetSubTreeType& subtree) {
8135b4aa86bSJames Feist                 if (ec)
8145b4aa86bSJames Feist                 {
8155b4aa86bSJames Feist                     BMCWEB_LOG_ERROR << ec;
816f12894f8SJason M. Bills                     messages::internalError(asyncResp->res);
8175b4aa86bSJames Feist                     return;
8185b4aa86bSJames Feist                 }
8195b4aa86bSJames Feist 
8205b4aa86bSJames Feist                 // create map of <connection, path to objMgr>>
8215b4aa86bSJames Feist                 boost::container::flat_map<std::string, std::string>
8225b4aa86bSJames Feist                     objectMgrPaths;
8236bce33bcSJames Feist                 boost::container::flat_set<std::string> calledConnections;
8245b4aa86bSJames Feist                 for (const auto& pathGroup : subtree)
8255b4aa86bSJames Feist                 {
8265b4aa86bSJames Feist                     for (const auto& connectionGroup : pathGroup.second)
8275b4aa86bSJames Feist                     {
8286bce33bcSJames Feist                         auto findConnection =
8296bce33bcSJames Feist                             calledConnections.find(connectionGroup.first);
8306bce33bcSJames Feist                         if (findConnection != calledConnections.end())
8316bce33bcSJames Feist                         {
8326bce33bcSJames Feist                             break;
8336bce33bcSJames Feist                         }
8345b4aa86bSJames Feist                         for (const std::string& interface :
8355b4aa86bSJames Feist                              connectionGroup.second)
8365b4aa86bSJames Feist                         {
8375b4aa86bSJames Feist                             if (interface == objectManagerIface)
8385b4aa86bSJames Feist                             {
8395b4aa86bSJames Feist                                 objectMgrPaths[connectionGroup.first] =
8405b4aa86bSJames Feist                                     pathGroup.first;
8415b4aa86bSJames Feist                             }
8425b4aa86bSJames Feist                             // this list is alphabetical, so we
8435b4aa86bSJames Feist                             // should have found the objMgr by now
8445b4aa86bSJames Feist                             if (interface == pidConfigurationIface ||
845b7a08d04SJames Feist                                 interface == pidZoneConfigurationIface ||
846b7a08d04SJames Feist                                 interface == stepwiseConfigurationIface)
8475b4aa86bSJames Feist                             {
8485b4aa86bSJames Feist                                 auto findObjMgr =
8495b4aa86bSJames Feist                                     objectMgrPaths.find(connectionGroup.first);
8505b4aa86bSJames Feist                                 if (findObjMgr == objectMgrPaths.end())
8515b4aa86bSJames Feist                                 {
8525b4aa86bSJames Feist                                     BMCWEB_LOG_DEBUG << connectionGroup.first
8535b4aa86bSJames Feist                                                      << "Has no Object Manager";
8545b4aa86bSJames Feist                                     continue;
8555b4aa86bSJames Feist                                 }
8566bce33bcSJames Feist 
8576bce33bcSJames Feist                                 calledConnections.insert(connectionGroup.first);
8586bce33bcSJames Feist 
8595b4aa86bSJames Feist                                 asyncPopulatePid(findObjMgr->first,
8605b4aa86bSJames Feist                                                  findObjMgr->second, asyncResp);
8615b4aa86bSJames Feist                                 break;
8625b4aa86bSJames Feist                             }
8635b4aa86bSJames Feist                         }
8645b4aa86bSJames Feist                     }
8655b4aa86bSJames Feist                 }
8665b4aa86bSJames Feist             },
8675b4aa86bSJames Feist             "xyz.openbmc_project.ObjectMapper",
8685b4aa86bSJames Feist             "/xyz/openbmc_project/object_mapper",
8695b4aa86bSJames Feist             "xyz.openbmc_project.ObjectMapper", "GetSubTree", "/", 0,
870b7a08d04SJames Feist             std::array<const char*, 4>{
871b7a08d04SJames Feist                 pidConfigurationIface, pidZoneConfigurationIface,
872b7a08d04SJames Feist                 objectManagerIface, stepwiseConfigurationIface});
8735b4aa86bSJames Feist     }
8745b4aa86bSJames Feist 
87555c7b7a2SEd Tanous     void doGet(crow::Response& res, const crow::Request& req,
8761abe55efSEd Tanous                const std::vector<std::string>& params) override
8771abe55efSEd Tanous     {
8780f74e643SEd Tanous         res.jsonValue["@odata.id"] = "/redfish/v1/Managers/bmc";
8790f74e643SEd Tanous         res.jsonValue["@odata.type"] = "#Manager.v1_3_0.Manager";
8800f74e643SEd Tanous         res.jsonValue["@odata.context"] =
8810f74e643SEd Tanous             "/redfish/v1/$metadata#Manager.Manager";
8820f74e643SEd Tanous         res.jsonValue["Id"] = "bmc";
8830f74e643SEd Tanous         res.jsonValue["Name"] = "OpenBmc Manager";
8840f74e643SEd Tanous         res.jsonValue["Description"] = "Baseboard Management Controller";
8850f74e643SEd Tanous         res.jsonValue["PowerState"] = "On";
886029573d4SEd Tanous         res.jsonValue["Status"] = {{"State", "Enabled"}, {"Health", "OK"}};
8870f74e643SEd Tanous         res.jsonValue["ManagerType"] = "BMC";
8887517658eSEd Tanous         res.jsonValue["UUID"] = uuid;
8890f74e643SEd Tanous         res.jsonValue["Model"] = "OpenBmc"; // TODO(ed), get model
8900f74e643SEd Tanous 
8910f74e643SEd Tanous         res.jsonValue["LogServices"] = {
8920f74e643SEd Tanous             {"@odata.id", "/redfish/v1/Managers/bmc/LogServices"}};
8930f74e643SEd Tanous 
8940f74e643SEd Tanous         res.jsonValue["NetworkProtocol"] = {
8950f74e643SEd Tanous             {"@odata.id", "/redfish/v1/Managers/bmc/NetworkProtocol"}};
8960f74e643SEd Tanous 
8970f74e643SEd Tanous         res.jsonValue["EthernetInterfaces"] = {
8980f74e643SEd Tanous             {"@odata.id", "/redfish/v1/Managers/bmc/EthernetInterfaces"}};
8990f74e643SEd Tanous         // default oem data
9000f74e643SEd Tanous         nlohmann::json& oem = res.jsonValue["Oem"];
9010f74e643SEd Tanous         nlohmann::json& oemOpenbmc = oem["OpenBmc"];
9020f74e643SEd Tanous         oem["@odata.type"] = "#OemManager.Oem";
9030f74e643SEd Tanous         oem["@odata.id"] = "/redfish/v1/Managers/bmc#/Oem";
9040f74e643SEd Tanous         oem["@odata.context"] = "/redfish/v1/$metadata#OemManager.Oem";
9050f74e643SEd Tanous         oemOpenbmc["@odata.type"] = "#OemManager.OpenBmc";
9060f74e643SEd Tanous         oemOpenbmc["@odata.id"] = "/redfish/v1/Managers/bmc#/Oem/OpenBmc";
9070f74e643SEd Tanous         oemOpenbmc["@odata.context"] =
9080f74e643SEd Tanous             "/redfish/v1/$metadata#OemManager.OpenBmc";
9090f74e643SEd Tanous 
910ed5befbdSJennifer Lee         // Update Actions object.
9110f74e643SEd Tanous         nlohmann::json& manager_reset =
9120f74e643SEd Tanous             res.jsonValue["Actions"]["#Manager.Reset"];
913ed5befbdSJennifer Lee         manager_reset["target"] =
914ed5befbdSJennifer Lee             "/redfish/v1/Managers/bmc/Actions/Manager.Reset";
915ed5befbdSJennifer Lee         manager_reset["ResetType@Redfish.AllowableValues"] = {
916ed5befbdSJennifer Lee             "GracefulRestart"};
917ca537928SJennifer Lee 
9180f74e643SEd Tanous         res.jsonValue["DateTime"] = getDateTime();
919029573d4SEd Tanous         res.jsonValue["Links"] = {
920029573d4SEd Tanous             {"ManagerForServers@odata.count", 1},
921029573d4SEd Tanous             {"ManagerForServers",
922029573d4SEd Tanous              {{{"@odata.id", "/redfish/v1/Systems/system"}}}},
923029573d4SEd Tanous             {"ManagerForServers", nlohmann::json::array()}};
924ed5befbdSJennifer Lee         std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
9255b4aa86bSJames Feist 
926ca537928SJennifer Lee         crow::connections::systemBus->async_method_call(
927ca537928SJennifer Lee             [asyncResp](const boost::system::error_code ec,
9285b4aa86bSJames Feist                         const dbus::utility::ManagedObjectType& resp) {
929ca537928SJennifer Lee                 if (ec)
930ca537928SJennifer Lee                 {
931ca537928SJennifer Lee                     BMCWEB_LOG_ERROR << "Error while getting Software Version";
932f12894f8SJason M. Bills                     messages::internalError(asyncResp->res);
933ca537928SJennifer Lee                     return;
934ca537928SJennifer Lee                 }
935ca537928SJennifer Lee 
936ca537928SJennifer Lee                 for (auto& objpath : resp)
937ca537928SJennifer Lee                 {
938ca537928SJennifer Lee                     for (auto& interface : objpath.second)
939ca537928SJennifer Lee                     {
9405f2caaefSJames Feist                         // If interface is
9415f2caaefSJames Feist                         // xyz.openbmc_project.Software.Version, this is
9425f2caaefSJames Feist                         // what we're looking for.
943ca537928SJennifer Lee                         if (interface.first ==
944ca537928SJennifer Lee                             "xyz.openbmc_project.Software.Version")
945ca537928SJennifer Lee                         {
946ca537928SJennifer Lee                             // Cut out everyting until last "/", ...
947ca537928SJennifer Lee                             for (auto& property : interface.second)
948ca537928SJennifer Lee                             {
949ca537928SJennifer Lee                                 if (property.first == "Version")
950ca537928SJennifer Lee                                 {
951ca537928SJennifer Lee                                     const std::string* value =
952abf2add6SEd Tanous                                         std::get_if<std::string>(
953abf2add6SEd Tanous                                             &property.second);
954ca537928SJennifer Lee                                     if (value == nullptr)
955ca537928SJennifer Lee                                     {
956ca537928SJennifer Lee                                         continue;
957ca537928SJennifer Lee                                     }
958ca537928SJennifer Lee                                     asyncResp->res
959ca537928SJennifer Lee                                         .jsonValue["FirmwareVersion"] = *value;
960ca537928SJennifer Lee                                 }
961ca537928SJennifer Lee                             }
962ca537928SJennifer Lee                         }
963ca537928SJennifer Lee                     }
964ca537928SJennifer Lee                 }
965ca537928SJennifer Lee             },
966ca537928SJennifer Lee             "xyz.openbmc_project.Software.BMC.Updater",
967ca537928SJennifer Lee             "/xyz/openbmc_project/software",
968ca537928SJennifer Lee             "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
9695b4aa86bSJames Feist         getPidValues(asyncResp);
9705b4aa86bSJames Feist     }
9715f2caaefSJames Feist     void setPidValues(std::shared_ptr<AsyncResp> response, nlohmann::json& data)
97283ff9ab6SJames Feist     {
9735f2caaefSJames Feist 
97483ff9ab6SJames Feist         // todo(james): might make sense to do a mapper call here if this
97583ff9ab6SJames Feist         // interface gets more traction
97683ff9ab6SJames Feist         crow::connections::systemBus->async_method_call(
97783ff9ab6SJames Feist             [response,
97883ff9ab6SJames Feist              data](const boost::system::error_code ec,
97983ff9ab6SJames Feist                    const dbus::utility::ManagedObjectType& managedObj) {
98083ff9ab6SJames Feist                 if (ec)
98183ff9ab6SJames Feist                 {
98283ff9ab6SJames Feist                     BMCWEB_LOG_ERROR << "Error communicating to Entity Manager";
98335a62c7cSJason M. Bills                     messages::internalError(response->res);
98483ff9ab6SJames Feist                     return;
98583ff9ab6SJames Feist                 }
9865f2caaefSJames Feist 
9875f2caaefSJames Feist                 // todo(james) mutable doesn't work with asio bindings
9885f2caaefSJames Feist                 nlohmann::json jsonData = data;
9895f2caaefSJames Feist 
9905f2caaefSJames Feist                 std::optional<nlohmann::json> pidControllers;
9915f2caaefSJames Feist                 std::optional<nlohmann::json> fanControllers;
9925f2caaefSJames Feist                 std::optional<nlohmann::json> fanZones;
9935f2caaefSJames Feist                 std::optional<nlohmann::json> stepwiseControllers;
9945f2caaefSJames Feist                 if (!redfish::json_util::readJson(
9955f2caaefSJames Feist                         jsonData, response->res, "PidControllers",
9965f2caaefSJames Feist                         pidControllers, "FanControllers", fanControllers,
9975f2caaefSJames Feist                         "FanZones", fanZones, "StepwiseControllers",
9985f2caaefSJames Feist                         stepwiseControllers))
99983ff9ab6SJames Feist                 {
10005f2caaefSJames Feist                     BMCWEB_LOG_ERROR << "Line:" << __LINE__
10015f2caaefSJames Feist                                      << ", Illegal Property "
10025f2caaefSJames Feist                                      << jsonData.dump();
100383ff9ab6SJames Feist                     return;
100483ff9ab6SJames Feist                 }
10055f2caaefSJames Feist                 std::array<
1006*43b761d0SEd Tanous                     std::pair<std::string, std::optional<nlohmann::json>*>, 4>
10075f2caaefSJames Feist                     sections = {
10085f2caaefSJames Feist                         std::make_pair("PidControllers", &pidControllers),
10095f2caaefSJames Feist                         std::make_pair("FanControllers", &fanControllers),
10105f2caaefSJames Feist                         std::make_pair("FanZones", &fanZones),
10115f2caaefSJames Feist                         std::make_pair("StepwiseControllers",
10125f2caaefSJames Feist                                        &stepwiseControllers)};
10135f2caaefSJames Feist 
10145f2caaefSJames Feist                 for (auto& containerPair : sections)
101583ff9ab6SJames Feist                 {
10165f2caaefSJames Feist                     auto& container = *(containerPair.second);
10175f2caaefSJames Feist                     if (!container)
10185f2caaefSJames Feist                     {
10195f2caaefSJames Feist                         continue;
10205f2caaefSJames Feist                     }
1021*43b761d0SEd Tanous                     std::string& type = containerPair.first;
10225f2caaefSJames Feist 
10235f2caaefSJames Feist                     for (auto& record : container->items())
10245f2caaefSJames Feist                     {
10255f2caaefSJames Feist                         const auto& name = record.key();
102683ff9ab6SJames Feist                         auto pathItr =
102783ff9ab6SJames Feist                             std::find_if(managedObj.begin(), managedObj.end(),
102883ff9ab6SJames Feist                                          [&name](const auto& obj) {
102983ff9ab6SJames Feist                                              return boost::algorithm::ends_with(
103083ff9ab6SJames Feist                                                  obj.first.str, name);
103183ff9ab6SJames Feist                                          });
103283ff9ab6SJames Feist                         boost::container::flat_map<
103383ff9ab6SJames Feist                             std::string, dbus::utility::DbusVariantType>
103483ff9ab6SJames Feist                             output;
103583ff9ab6SJames Feist 
103683ff9ab6SJames Feist                         output.reserve(16); // The pid interface length
103783ff9ab6SJames Feist 
103883ff9ab6SJames Feist                         // determines if we're patching entity-manager or
103983ff9ab6SJames Feist                         // creating a new object
104083ff9ab6SJames Feist                         bool createNewObject = (pathItr == managedObj.end());
10415f2caaefSJames Feist                         std::string iface;
10425f2caaefSJames Feist                         if (type == "PidControllers" ||
10435f2caaefSJames Feist                             type == "FanControllers")
104483ff9ab6SJames Feist                         {
10455f2caaefSJames Feist                             iface = pidConfigurationIface;
104683ff9ab6SJames Feist                             if (!createNewObject &&
104783ff9ab6SJames Feist                                 pathItr->second.find(pidConfigurationIface) ==
104883ff9ab6SJames Feist                                     pathItr->second.end())
104983ff9ab6SJames Feist                             {
105083ff9ab6SJames Feist                                 createNewObject = true;
105183ff9ab6SJames Feist                             }
105283ff9ab6SJames Feist                         }
10535f2caaefSJames Feist                         else if (type == "FanZones")
10545f2caaefSJames Feist                         {
10555f2caaefSJames Feist                             iface = pidZoneConfigurationIface;
10565f2caaefSJames Feist                             if (!createNewObject &&
105783ff9ab6SJames Feist                                 pathItr->second.find(
105883ff9ab6SJames Feist                                     pidZoneConfigurationIface) ==
105983ff9ab6SJames Feist                                     pathItr->second.end())
106083ff9ab6SJames Feist                             {
10615f2caaefSJames Feist 
106283ff9ab6SJames Feist                                 createNewObject = true;
106383ff9ab6SJames Feist                             }
10645f2caaefSJames Feist                         }
10655f2caaefSJames Feist                         else if (type == "StepwiseControllers")
10665f2caaefSJames Feist                         {
10675f2caaefSJames Feist                             iface = stepwiseConfigurationIface;
10685f2caaefSJames Feist                             if (!createNewObject &&
10695f2caaefSJames Feist                                 pathItr->second.find(
10705f2caaefSJames Feist                                     stepwiseConfigurationIface) ==
10715f2caaefSJames Feist                                     pathItr->second.end())
10725f2caaefSJames Feist                             {
10735f2caaefSJames Feist                                 createNewObject = true;
10745f2caaefSJames Feist                             }
10755f2caaefSJames Feist                         }
107683ff9ab6SJames Feist                         output["Name"] =
107783ff9ab6SJames Feist                             boost::replace_all_copy(name, "_", " ");
107883ff9ab6SJames Feist 
107983ff9ab6SJames Feist                         std::string chassis;
108083ff9ab6SJames Feist                         CreatePIDRet ret = createPidInterface(
10815f2caaefSJames Feist                             response, type, std::move(record.value()),
108283ff9ab6SJames Feist                             pathItr->first.str, managedObj, createNewObject,
108383ff9ab6SJames Feist                             output, chassis);
108483ff9ab6SJames Feist                         if (ret == CreatePIDRet::fail)
108583ff9ab6SJames Feist                         {
108683ff9ab6SJames Feist                             return;
108783ff9ab6SJames Feist                         }
108883ff9ab6SJames Feist                         else if (ret == CreatePIDRet::del)
108983ff9ab6SJames Feist                         {
109083ff9ab6SJames Feist                             continue;
109183ff9ab6SJames Feist                         }
109283ff9ab6SJames Feist 
109383ff9ab6SJames Feist                         if (!createNewObject)
109483ff9ab6SJames Feist                         {
109583ff9ab6SJames Feist                             for (const auto& property : output)
109683ff9ab6SJames Feist                             {
109783ff9ab6SJames Feist                                 crow::connections::systemBus->async_method_call(
109883ff9ab6SJames Feist                                     [response,
109983ff9ab6SJames Feist                                      propertyName{std::string(property.first)}](
110083ff9ab6SJames Feist                                         const boost::system::error_code ec) {
110183ff9ab6SJames Feist                                         if (ec)
110283ff9ab6SJames Feist                                         {
110383ff9ab6SJames Feist                                             BMCWEB_LOG_ERROR
110483ff9ab6SJames Feist                                                 << "Error patching "
110583ff9ab6SJames Feist                                                 << propertyName << ": " << ec;
110635a62c7cSJason M. Bills                                             messages::internalError(
110735a62c7cSJason M. Bills                                                 response->res);
110883ff9ab6SJames Feist                                         }
110983ff9ab6SJames Feist                                     },
111083ff9ab6SJames Feist                                     "xyz.openbmc_project.EntityManager",
111183ff9ab6SJames Feist                                     pathItr->first.str,
111283ff9ab6SJames Feist                                     "org.freedesktop.DBus.Properties", "Set",
11135f2caaefSJames Feist                                     iface, property.first, property.second);
111483ff9ab6SJames Feist                             }
111583ff9ab6SJames Feist                         }
111683ff9ab6SJames Feist                         else
111783ff9ab6SJames Feist                         {
111883ff9ab6SJames Feist                             if (chassis.empty())
111983ff9ab6SJames Feist                             {
112083ff9ab6SJames Feist                                 BMCWEB_LOG_ERROR
112183ff9ab6SJames Feist                                     << "Failed to get chassis from config";
112235a62c7cSJason M. Bills                                 messages::invalidObject(response->res, name);
112383ff9ab6SJames Feist                                 return;
112483ff9ab6SJames Feist                             }
112583ff9ab6SJames Feist 
112683ff9ab6SJames Feist                             bool foundChassis = false;
112783ff9ab6SJames Feist                             for (const auto& obj : managedObj)
112883ff9ab6SJames Feist                             {
112983ff9ab6SJames Feist                                 if (boost::algorithm::ends_with(obj.first.str,
113083ff9ab6SJames Feist                                                                 chassis))
113183ff9ab6SJames Feist                                 {
113283ff9ab6SJames Feist                                     chassis = obj.first.str;
113383ff9ab6SJames Feist                                     foundChassis = true;
113483ff9ab6SJames Feist                                     break;
113583ff9ab6SJames Feist                                 }
113683ff9ab6SJames Feist                             }
113783ff9ab6SJames Feist                             if (!foundChassis)
113883ff9ab6SJames Feist                             {
113983ff9ab6SJames Feist                                 BMCWEB_LOG_ERROR
114083ff9ab6SJames Feist                                     << "Failed to find chassis on dbus";
114183ff9ab6SJames Feist                                 messages::resourceMissingAtURI(
114235a62c7cSJason M. Bills                                     response->res,
114335a62c7cSJason M. Bills                                     "/redfish/v1/Chassis/" + chassis);
114483ff9ab6SJames Feist                                 return;
114583ff9ab6SJames Feist                             }
114683ff9ab6SJames Feist 
114783ff9ab6SJames Feist                             crow::connections::systemBus->async_method_call(
114883ff9ab6SJames Feist                                 [response](const boost::system::error_code ec) {
114983ff9ab6SJames Feist                                     if (ec)
115083ff9ab6SJames Feist                                     {
115183ff9ab6SJames Feist                                         BMCWEB_LOG_ERROR
115283ff9ab6SJames Feist                                             << "Error Adding Pid Object " << ec;
115335a62c7cSJason M. Bills                                         messages::internalError(response->res);
115483ff9ab6SJames Feist                                     }
115583ff9ab6SJames Feist                                 },
115683ff9ab6SJames Feist                                 "xyz.openbmc_project.EntityManager", chassis,
115783ff9ab6SJames Feist                                 "xyz.openbmc_project.AddObject", "AddObject",
115883ff9ab6SJames Feist                                 output);
115983ff9ab6SJames Feist                         }
116083ff9ab6SJames Feist                     }
116183ff9ab6SJames Feist                 }
116283ff9ab6SJames Feist             },
116383ff9ab6SJames Feist             "xyz.openbmc_project.EntityManager", "/", objectManagerIface,
116483ff9ab6SJames Feist             "GetManagedObjects");
116583ff9ab6SJames Feist     }
11665b4aa86bSJames Feist 
11675b4aa86bSJames Feist     void doPatch(crow::Response& res, const crow::Request& req,
11685b4aa86bSJames Feist                  const std::vector<std::string>& params) override
11695b4aa86bSJames Feist     {
11700627a2c7SEd Tanous         std::optional<nlohmann::json> oem;
11710627a2c7SEd Tanous 
11720627a2c7SEd Tanous         if (!json_util::readJson(req, res, "Oem", oem))
117383ff9ab6SJames Feist         {
117483ff9ab6SJames Feist             return;
117583ff9ab6SJames Feist         }
11760627a2c7SEd Tanous 
117783ff9ab6SJames Feist         std::shared_ptr<AsyncResp> response = std::make_shared<AsyncResp>(res);
11780627a2c7SEd Tanous 
11790627a2c7SEd Tanous         if (oem)
118083ff9ab6SJames Feist         {
11815f2caaefSJames Feist             std::optional<nlohmann::json> openbmc;
1182*43b761d0SEd Tanous             if (!redfish::json_util::readJson(*oem, res, "OpenBmc", openbmc))
118383ff9ab6SJames Feist             {
1184*43b761d0SEd Tanous                 BMCWEB_LOG_ERROR << "Line:" << __LINE__ << ", Illegal Property "
1185*43b761d0SEd Tanous                                  << oem->dump();
118683ff9ab6SJames Feist                 return;
118783ff9ab6SJames Feist             }
11885f2caaefSJames Feist             if (openbmc)
118983ff9ab6SJames Feist             {
11905f2caaefSJames Feist                 std::optional<nlohmann::json> fan;
1191*43b761d0SEd Tanous                 if (!redfish::json_util::readJson(*openbmc, res, "Fan", fan))
119283ff9ab6SJames Feist                 {
11935f2caaefSJames Feist                     BMCWEB_LOG_ERROR << "Line:" << __LINE__
11945f2caaefSJames Feist                                      << ", Illegal Property "
11955f2caaefSJames Feist                                      << openbmc->dump();
119683ff9ab6SJames Feist                     return;
119783ff9ab6SJames Feist                 }
11985f2caaefSJames Feist                 if (fan)
119983ff9ab6SJames Feist                 {
12005f2caaefSJames Feist                     setPidValues(response, *fan);
120183ff9ab6SJames Feist                 }
120283ff9ab6SJames Feist             }
120383ff9ab6SJames Feist         }
120483ff9ab6SJames Feist     }
12059c310685SBorawski.Lukasz 
12061abe55efSEd Tanous     std::string getDateTime() const
12071abe55efSEd Tanous     {
12089c310685SBorawski.Lukasz         std::array<char, 128> dateTime;
12099c310685SBorawski.Lukasz         std::string redfishDateTime("0000-00-00T00:00:00Z00:00");
12109c310685SBorawski.Lukasz         std::time_t time = std::time(nullptr);
12119c310685SBorawski.Lukasz 
12129c310685SBorawski.Lukasz         if (std::strftime(dateTime.begin(), dateTime.size(), "%FT%T%z",
12131abe55efSEd Tanous                           std::localtime(&time)))
12141abe55efSEd Tanous         {
12159c310685SBorawski.Lukasz             // insert the colon required by the ISO 8601 standard
12169c310685SBorawski.Lukasz             redfishDateTime = std::string(dateTime.data());
12179c310685SBorawski.Lukasz             redfishDateTime.insert(redfishDateTime.end() - 2, ':');
12189c310685SBorawski.Lukasz         }
12199c310685SBorawski.Lukasz 
12209c310685SBorawski.Lukasz         return redfishDateTime;
12219c310685SBorawski.Lukasz     }
12220f74e643SEd Tanous 
12230f74e643SEd Tanous     std::string uuid;
12249c310685SBorawski.Lukasz };
12259c310685SBorawski.Lukasz 
12261abe55efSEd Tanous class ManagerCollection : public Node
12271abe55efSEd Tanous {
12289c310685SBorawski.Lukasz   public:
12291abe55efSEd Tanous     ManagerCollection(CrowApp& app) : Node(app, "/redfish/v1/Managers/")
12301abe55efSEd Tanous     {
1231a434f2bdSEd Tanous         entityPrivileges = {
1232a434f2bdSEd Tanous             {boost::beast::http::verb::get, {{"Login"}}},
1233e0d918bcSEd Tanous             {boost::beast::http::verb::head, {{"Login"}}},
1234e0d918bcSEd Tanous             {boost::beast::http::verb::patch, {{"ConfigureManager"}}},
1235e0d918bcSEd Tanous             {boost::beast::http::verb::put, {{"ConfigureManager"}}},
1236e0d918bcSEd Tanous             {boost::beast::http::verb::delete_, {{"ConfigureManager"}}},
1237e0d918bcSEd Tanous             {boost::beast::http::verb::post, {{"ConfigureManager"}}}};
12389c310685SBorawski.Lukasz     }
12399c310685SBorawski.Lukasz 
12409c310685SBorawski.Lukasz   private:
124155c7b7a2SEd Tanous     void doGet(crow::Response& res, const crow::Request& req,
12421abe55efSEd Tanous                const std::vector<std::string>& params) override
12431abe55efSEd Tanous     {
124483ff9ab6SJames Feist         // Collections don't include the static data added by SubRoute
124583ff9ab6SJames Feist         // because it has a duplicate entry for members
124655c7b7a2SEd Tanous         res.jsonValue["@odata.id"] = "/redfish/v1/Managers";
124755c7b7a2SEd Tanous         res.jsonValue["@odata.type"] = "#ManagerCollection.ManagerCollection";
124855c7b7a2SEd Tanous         res.jsonValue["@odata.context"] =
124955c7b7a2SEd Tanous             "/redfish/v1/$metadata#ManagerCollection.ManagerCollection";
125055c7b7a2SEd Tanous         res.jsonValue["Name"] = "Manager Collection";
125155c7b7a2SEd Tanous         res.jsonValue["Members@odata.count"] = 1;
125255c7b7a2SEd Tanous         res.jsonValue["Members"] = {
12535b4aa86bSJames Feist             {{"@odata.id", "/redfish/v1/Managers/bmc"}}};
12549c310685SBorawski.Lukasz         res.end();
12559c310685SBorawski.Lukasz     }
12569c310685SBorawski.Lukasz };
12579c310685SBorawski.Lukasz } // namespace redfish
1258