xref: /openbmc/bmcweb/features/redfish/lib/managers.hpp (revision f97ddba7015a553f13639233f7e5d3fb0152d0b2)
19c310685SBorawski.Lukasz /*
29c310685SBorawski.Lukasz // Copyright (c) 2018 Intel Corporation
39c310685SBorawski.Lukasz //
49c310685SBorawski.Lukasz // Licensed under the Apache License, Version 2.0 (the "License");
59c310685SBorawski.Lukasz // you may not use this file except in compliance with the License.
69c310685SBorawski.Lukasz // You may obtain a copy of the License at
79c310685SBorawski.Lukasz //
89c310685SBorawski.Lukasz //      http://www.apache.org/licenses/LICENSE-2.0
99c310685SBorawski.Lukasz //
109c310685SBorawski.Lukasz // Unless required by applicable law or agreed to in writing, software
119c310685SBorawski.Lukasz // distributed under the License is distributed on an "AS IS" BASIS,
129c310685SBorawski.Lukasz // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
139c310685SBorawski.Lukasz // See the License for the specific language governing permissions and
149c310685SBorawski.Lukasz // limitations under the License.
159c310685SBorawski.Lukasz */
169c310685SBorawski.Lukasz #pragma once
179c310685SBorawski.Lukasz 
18b49ac873SJames Feist #include "health.hpp"
199c310685SBorawski.Lukasz #include "node.hpp"
20c5d03ff4SJennifer Lee #include "redfish_util.hpp"
219c310685SBorawski.Lukasz 
225b4aa86bSJames Feist #include <boost/algorithm/string/replace.hpp>
23af5d6058SSantosh Puranik #include <boost/date_time.hpp>
245b4aa86bSJames Feist #include <dbus_utility.hpp>
25e90c5052SAndrew Geissler #include <utils/fw_utils.hpp>
267bffdb7eSBernard Wong #include <utils/systemd_utils.hpp>
271214b7e7SGunnar Mills 
284bfefa74SGunnar Mills #include <cstdint>
291214b7e7SGunnar Mills #include <memory>
301214b7e7SGunnar Mills #include <sstream>
31abf2add6SEd Tanous #include <variant>
325b4aa86bSJames Feist 
331abe55efSEd Tanous namespace redfish
341abe55efSEd Tanous {
35ed5befbdSJennifer Lee 
36ed5befbdSJennifer Lee /**
372a5c4407SGunnar Mills  * Function reboots the BMC.
382a5c4407SGunnar Mills  *
392a5c4407SGunnar Mills  * @param[in] asyncResp - Shared pointer for completing asynchronous calls
40ed5befbdSJennifer Lee  */
4123a21a1cSEd Tanous inline void doBMCGracefulRestart(std::shared_ptr<AsyncResp> asyncResp)
42ed5befbdSJennifer Lee {
43ed5befbdSJennifer Lee     const char* processName = "xyz.openbmc_project.State.BMC";
44ed5befbdSJennifer Lee     const char* objectPath = "/xyz/openbmc_project/state/bmc0";
45ed5befbdSJennifer Lee     const char* interfaceName = "xyz.openbmc_project.State.BMC";
46ed5befbdSJennifer Lee     const std::string& propertyValue =
47ed5befbdSJennifer Lee         "xyz.openbmc_project.State.BMC.Transition.Reboot";
48ed5befbdSJennifer Lee     const char* destProperty = "RequestedBMCTransition";
49ed5befbdSJennifer Lee 
50ed5befbdSJennifer Lee     // Create the D-Bus variant for D-Bus call.
51ed5befbdSJennifer Lee     VariantType dbusPropertyValue(propertyValue);
52ed5befbdSJennifer Lee 
53ed5befbdSJennifer Lee     crow::connections::systemBus->async_method_call(
54ed5befbdSJennifer Lee         [asyncResp](const boost::system::error_code ec) {
55ed5befbdSJennifer Lee             // Use "Set" method to set the property value.
56ed5befbdSJennifer Lee             if (ec)
57ed5befbdSJennifer Lee             {
582a5c4407SGunnar Mills                 BMCWEB_LOG_DEBUG << "[Set] Bad D-Bus request error: " << ec;
59ed5befbdSJennifer Lee                 messages::internalError(asyncResp->res);
60ed5befbdSJennifer Lee                 return;
61ed5befbdSJennifer Lee             }
62ed5befbdSJennifer Lee 
63ed5befbdSJennifer Lee             messages::success(asyncResp->res);
64ed5befbdSJennifer Lee         },
65ed5befbdSJennifer Lee         processName, objectPath, "org.freedesktop.DBus.Properties", "Set",
66ed5befbdSJennifer Lee         interfaceName, destProperty, dbusPropertyValue);
67ed5befbdSJennifer Lee }
682a5c4407SGunnar Mills 
69f92af389SJayaprakash Mutyala void doBMCForceRestart(std::shared_ptr<AsyncResp> asyncResp)
70f92af389SJayaprakash Mutyala {
71f92af389SJayaprakash Mutyala     const char* processName = "xyz.openbmc_project.State.BMC";
72f92af389SJayaprakash Mutyala     const char* objectPath = "/xyz/openbmc_project/state/bmc0";
73f92af389SJayaprakash Mutyala     const char* interfaceName = "xyz.openbmc_project.State.BMC";
74f92af389SJayaprakash Mutyala     const std::string& propertyValue =
75f92af389SJayaprakash Mutyala         "xyz.openbmc_project.State.BMC.Transition.HardReboot";
76f92af389SJayaprakash Mutyala     const char* destProperty = "RequestedBMCTransition";
77f92af389SJayaprakash Mutyala 
78f92af389SJayaprakash Mutyala     // Create the D-Bus variant for D-Bus call.
79f92af389SJayaprakash Mutyala     VariantType dbusPropertyValue(propertyValue);
80f92af389SJayaprakash Mutyala 
81f92af389SJayaprakash Mutyala     crow::connections::systemBus->async_method_call(
82f92af389SJayaprakash Mutyala         [asyncResp](const boost::system::error_code ec) {
83f92af389SJayaprakash Mutyala             // Use "Set" method to set the property value.
84f92af389SJayaprakash Mutyala             if (ec)
85f92af389SJayaprakash Mutyala             {
86f92af389SJayaprakash Mutyala                 BMCWEB_LOG_DEBUG << "[Set] Bad D-Bus request error: " << ec;
87f92af389SJayaprakash Mutyala                 messages::internalError(asyncResp->res);
88f92af389SJayaprakash Mutyala                 return;
89f92af389SJayaprakash Mutyala             }
90f92af389SJayaprakash Mutyala 
91f92af389SJayaprakash Mutyala             messages::success(asyncResp->res);
92f92af389SJayaprakash Mutyala         },
93f92af389SJayaprakash Mutyala         processName, objectPath, "org.freedesktop.DBus.Properties", "Set",
94f92af389SJayaprakash Mutyala         interfaceName, destProperty, dbusPropertyValue);
95f92af389SJayaprakash Mutyala }
96f92af389SJayaprakash Mutyala 
972a5c4407SGunnar Mills /**
982a5c4407SGunnar Mills  * ManagerResetAction class supports the POST method for the Reset (reboot)
992a5c4407SGunnar Mills  * action.
1002a5c4407SGunnar Mills  */
1012a5c4407SGunnar Mills class ManagerResetAction : public Node
1022a5c4407SGunnar Mills {
1032a5c4407SGunnar Mills   public:
10452cc112dSEd Tanous     ManagerResetAction(App& app) :
1052a5c4407SGunnar Mills         Node(app, "/redfish/v1/Managers/bmc/Actions/Manager.Reset/")
1062a5c4407SGunnar Mills     {
1072a5c4407SGunnar Mills         entityPrivileges = {
1082a5c4407SGunnar Mills             {boost::beast::http::verb::post, {{"ConfigureManager"}}}};
1092a5c4407SGunnar Mills     }
1102a5c4407SGunnar Mills 
1112a5c4407SGunnar Mills   private:
1122a5c4407SGunnar Mills     /**
1132a5c4407SGunnar Mills      * Function handles POST method request.
1142a5c4407SGunnar Mills      * Analyzes POST body before sending Reset (Reboot) request data to D-Bus.
115f92af389SJayaprakash Mutyala      * OpenBMC supports ResetType "GracefulRestart" and "ForceRestart".
1162a5c4407SGunnar Mills      */
1172a5c4407SGunnar Mills     void doPost(crow::Response& res, const crow::Request& req,
118cb13a392SEd Tanous                 const std::vector<std::string>&) override
1192a5c4407SGunnar Mills     {
1202a5c4407SGunnar Mills         BMCWEB_LOG_DEBUG << "Post Manager Reset.";
1212a5c4407SGunnar Mills 
1222a5c4407SGunnar Mills         std::string resetType;
1232a5c4407SGunnar Mills         auto asyncResp = std::make_shared<AsyncResp>(res);
1242a5c4407SGunnar Mills 
1252a5c4407SGunnar Mills         if (!json_util::readJson(req, asyncResp->res, "ResetType", resetType))
1262a5c4407SGunnar Mills         {
1272a5c4407SGunnar Mills             return;
1282a5c4407SGunnar Mills         }
1292a5c4407SGunnar Mills 
130f92af389SJayaprakash Mutyala         if (resetType == "GracefulRestart")
131f92af389SJayaprakash Mutyala         {
132f92af389SJayaprakash Mutyala             BMCWEB_LOG_DEBUG << "Proceeding with " << resetType;
133f92af389SJayaprakash Mutyala             doBMCGracefulRestart(asyncResp);
134f92af389SJayaprakash Mutyala             return;
135f92af389SJayaprakash Mutyala         }
136f92af389SJayaprakash Mutyala         else if (resetType == "ForceRestart")
137f92af389SJayaprakash Mutyala         {
138f92af389SJayaprakash Mutyala             BMCWEB_LOG_DEBUG << "Proceeding with " << resetType;
139f92af389SJayaprakash Mutyala             doBMCForceRestart(asyncResp);
140f92af389SJayaprakash Mutyala             return;
141f92af389SJayaprakash Mutyala         }
142f92af389SJayaprakash Mutyala         else
1432a5c4407SGunnar Mills         {
1442a5c4407SGunnar Mills             BMCWEB_LOG_DEBUG << "Invalid property value for ResetType: "
1452a5c4407SGunnar Mills                              << resetType;
1462a5c4407SGunnar Mills             messages::actionParameterNotSupported(asyncResp->res, resetType,
1472a5c4407SGunnar Mills                                                   "ResetType");
1482a5c4407SGunnar Mills 
1492a5c4407SGunnar Mills             return;
1502a5c4407SGunnar Mills         }
1512a5c4407SGunnar Mills     }
152ed5befbdSJennifer Lee };
153ed5befbdSJennifer Lee 
1543e40fc74SGunnar Mills /**
1553e40fc74SGunnar Mills  * ManagerResetToDefaultsAction class supports POST method for factory reset
1563e40fc74SGunnar Mills  * action.
1573e40fc74SGunnar Mills  */
1583e40fc74SGunnar Mills class ManagerResetToDefaultsAction : public Node
1593e40fc74SGunnar Mills {
1603e40fc74SGunnar Mills   public:
16152cc112dSEd Tanous     ManagerResetToDefaultsAction(App& app) :
1623e40fc74SGunnar Mills         Node(app, "/redfish/v1/Managers/bmc/Actions/Manager.ResetToDefaults/")
1633e40fc74SGunnar Mills     {
1643e40fc74SGunnar Mills         entityPrivileges = {
1653e40fc74SGunnar Mills             {boost::beast::http::verb::post, {{"ConfigureManager"}}}};
1663e40fc74SGunnar Mills     }
1673e40fc74SGunnar Mills 
1683e40fc74SGunnar Mills   private:
1693e40fc74SGunnar Mills     /**
1703e40fc74SGunnar Mills      * Function handles ResetToDefaults POST method request.
1713e40fc74SGunnar Mills      *
1723e40fc74SGunnar Mills      * Analyzes POST body message and factory resets BMC by calling
1733e40fc74SGunnar Mills      * BMC code updater factory reset followed by a BMC reboot.
1743e40fc74SGunnar Mills      *
1753e40fc74SGunnar Mills      * BMC code updater factory reset wipes the whole BMC read-write
1763e40fc74SGunnar Mills      * filesystem which includes things like the network settings.
1773e40fc74SGunnar Mills      *
1783e40fc74SGunnar Mills      * OpenBMC only supports ResetToDefaultsType "ResetAll".
1793e40fc74SGunnar Mills      */
1803e40fc74SGunnar Mills     void doPost(crow::Response& res, const crow::Request& req,
181cb13a392SEd Tanous                 const std::vector<std::string>&) override
1823e40fc74SGunnar Mills     {
1833e40fc74SGunnar Mills         BMCWEB_LOG_DEBUG << "Post ResetToDefaults.";
1843e40fc74SGunnar Mills 
1853e40fc74SGunnar Mills         std::string resetType;
1863e40fc74SGunnar Mills         auto asyncResp = std::make_shared<AsyncResp>(res);
1873e40fc74SGunnar Mills 
1883e40fc74SGunnar Mills         if (!json_util::readJson(req, asyncResp->res, "ResetToDefaultsType",
1893e40fc74SGunnar Mills                                  resetType))
1903e40fc74SGunnar Mills         {
1913e40fc74SGunnar Mills             BMCWEB_LOG_DEBUG << "Missing property ResetToDefaultsType.";
1923e40fc74SGunnar Mills 
1933e40fc74SGunnar Mills             messages::actionParameterMissing(asyncResp->res, "ResetToDefaults",
1943e40fc74SGunnar Mills                                              "ResetToDefaultsType");
1953e40fc74SGunnar Mills             return;
1963e40fc74SGunnar Mills         }
1973e40fc74SGunnar Mills 
1983e40fc74SGunnar Mills         if (resetType != "ResetAll")
1993e40fc74SGunnar Mills         {
2003e40fc74SGunnar Mills             BMCWEB_LOG_DEBUG << "Invalid property value for "
2013e40fc74SGunnar Mills                                 "ResetToDefaultsType: "
2023e40fc74SGunnar Mills                              << resetType;
2033e40fc74SGunnar Mills             messages::actionParameterNotSupported(asyncResp->res, resetType,
2043e40fc74SGunnar Mills                                                   "ResetToDefaultsType");
2053e40fc74SGunnar Mills             return;
2063e40fc74SGunnar Mills         }
2073e40fc74SGunnar Mills 
2083e40fc74SGunnar Mills         crow::connections::systemBus->async_method_call(
2093e40fc74SGunnar Mills             [asyncResp](const boost::system::error_code ec) {
2103e40fc74SGunnar Mills                 if (ec)
2113e40fc74SGunnar Mills                 {
2123e40fc74SGunnar Mills                     BMCWEB_LOG_DEBUG << "Failed to ResetToDefaults: " << ec;
2133e40fc74SGunnar Mills                     messages::internalError(asyncResp->res);
2143e40fc74SGunnar Mills                     return;
2153e40fc74SGunnar Mills                 }
2163e40fc74SGunnar Mills                 // Factory Reset doesn't actually happen until a reboot
2173e40fc74SGunnar Mills                 // Can't erase what the BMC is running on
2183e40fc74SGunnar Mills                 doBMCGracefulRestart(asyncResp);
2193e40fc74SGunnar Mills             },
2203e40fc74SGunnar Mills             "xyz.openbmc_project.Software.BMC.Updater",
2213e40fc74SGunnar Mills             "/xyz/openbmc_project/software",
2223e40fc74SGunnar Mills             "xyz.openbmc_project.Common.FactoryReset", "Reset");
2233e40fc74SGunnar Mills     }
2243e40fc74SGunnar Mills };
2253e40fc74SGunnar Mills 
2261cb1a9e6SAppaRao Puli /**
2271cb1a9e6SAppaRao Puli  * ManagerResetActionInfo derived class for delivering Manager
2281cb1a9e6SAppaRao Puli  * ResetType AllowableValues using ResetInfo schema.
2291cb1a9e6SAppaRao Puli  */
2301cb1a9e6SAppaRao Puli class ManagerResetActionInfo : public Node
2311cb1a9e6SAppaRao Puli {
2321cb1a9e6SAppaRao Puli   public:
2331cb1a9e6SAppaRao Puli     /*
2341cb1a9e6SAppaRao Puli      * Default Constructor
2351cb1a9e6SAppaRao Puli      */
23652cc112dSEd Tanous     ManagerResetActionInfo(App& app) :
2371cb1a9e6SAppaRao Puli         Node(app, "/redfish/v1/Managers/bmc/ResetActionInfo/")
2381cb1a9e6SAppaRao Puli     {
2391cb1a9e6SAppaRao Puli         entityPrivileges = {
2401cb1a9e6SAppaRao Puli             {boost::beast::http::verb::get, {{"Login"}}},
2411cb1a9e6SAppaRao Puli             {boost::beast::http::verb::head, {{"Login"}}},
2421cb1a9e6SAppaRao Puli             {boost::beast::http::verb::patch, {{"ConfigureComponents"}}},
2431cb1a9e6SAppaRao Puli             {boost::beast::http::verb::put, {{"ConfigureComponents"}}},
2441cb1a9e6SAppaRao Puli             {boost::beast::http::verb::delete_, {{"ConfigureComponents"}}},
2451cb1a9e6SAppaRao Puli             {boost::beast::http::verb::post, {{"ConfigureComponents"}}}};
2461cb1a9e6SAppaRao Puli     }
2471cb1a9e6SAppaRao Puli 
2481cb1a9e6SAppaRao Puli   private:
2491cb1a9e6SAppaRao Puli     /**
2501cb1a9e6SAppaRao Puli      * Functions triggers appropriate requests on DBus
2511cb1a9e6SAppaRao Puli      */
252cb13a392SEd Tanous     void doGet(crow::Response& res, const crow::Request&,
253cb13a392SEd Tanous                const std::vector<std::string>&) override
2541cb1a9e6SAppaRao Puli     {
2551cb1a9e6SAppaRao Puli         res.jsonValue = {
2561cb1a9e6SAppaRao Puli             {"@odata.type", "#ActionInfo.v1_1_2.ActionInfo"},
2571cb1a9e6SAppaRao Puli             {"@odata.id", "/redfish/v1/Managers/bmc/ResetActionInfo"},
2581cb1a9e6SAppaRao Puli             {"Name", "Reset Action Info"},
2591cb1a9e6SAppaRao Puli             {"Id", "ResetActionInfo"},
2601cb1a9e6SAppaRao Puli             {"Parameters",
2611cb1a9e6SAppaRao Puli              {{{"Name", "ResetType"},
2621cb1a9e6SAppaRao Puli                {"Required", true},
2631cb1a9e6SAppaRao Puli                {"DataType", "String"},
264f92af389SJayaprakash Mutyala                {"AllowableValues", {"GracefulRestart", "ForceRestart"}}}}}};
2651cb1a9e6SAppaRao Puli         res.end();
2661cb1a9e6SAppaRao Puli     }
2671cb1a9e6SAppaRao Puli };
2681cb1a9e6SAppaRao Puli 
2695b4aa86bSJames Feist static constexpr const char* objectManagerIface =
2705b4aa86bSJames Feist     "org.freedesktop.DBus.ObjectManager";
2715b4aa86bSJames Feist static constexpr const char* pidConfigurationIface =
2725b4aa86bSJames Feist     "xyz.openbmc_project.Configuration.Pid";
2735b4aa86bSJames Feist static constexpr const char* pidZoneConfigurationIface =
2745b4aa86bSJames Feist     "xyz.openbmc_project.Configuration.Pid.Zone";
275b7a08d04SJames Feist static constexpr const char* stepwiseConfigurationIface =
276b7a08d04SJames Feist     "xyz.openbmc_project.Configuration.Stepwise";
27773df0db0SJames Feist static constexpr const char* thermalModeIface =
27873df0db0SJames Feist     "xyz.openbmc_project.Control.ThermalMode";
2799c310685SBorawski.Lukasz 
28023a21a1cSEd Tanous inline void asyncPopulatePid(const std::string& connection,
2815b4aa86bSJames Feist                              const std::string& path,
28273df0db0SJames Feist                              const std::string& currentProfile,
28373df0db0SJames Feist                              const std::vector<std::string>& supportedProfiles,
2845b4aa86bSJames Feist                              std::shared_ptr<AsyncResp> asyncResp)
2855b4aa86bSJames Feist {
2865b4aa86bSJames Feist 
2875b4aa86bSJames Feist     crow::connections::systemBus->async_method_call(
28873df0db0SJames Feist         [asyncResp, currentProfile, supportedProfiles](
28973df0db0SJames Feist             const boost::system::error_code ec,
2905b4aa86bSJames Feist             const dbus::utility::ManagedObjectType& managedObj) {
2915b4aa86bSJames Feist             if (ec)
2925b4aa86bSJames Feist             {
2935b4aa86bSJames Feist                 BMCWEB_LOG_ERROR << ec;
2945b4aa86bSJames Feist                 asyncResp->res.jsonValue.clear();
295f12894f8SJason M. Bills                 messages::internalError(asyncResp->res);
2965b4aa86bSJames Feist                 return;
2975b4aa86bSJames Feist             }
2985b4aa86bSJames Feist             nlohmann::json& configRoot =
2995b4aa86bSJames Feist                 asyncResp->res.jsonValue["Oem"]["OpenBmc"]["Fan"];
3005b4aa86bSJames Feist             nlohmann::json& fans = configRoot["FanControllers"];
3015b4aa86bSJames Feist             fans["@odata.type"] = "#OemManager.FanControllers";
3025b4aa86bSJames Feist             fans["@odata.id"] = "/redfish/v1/Managers/bmc#/Oem/OpenBmc/"
3035b4aa86bSJames Feist                                 "Fan/FanControllers";
3045b4aa86bSJames Feist 
3055b4aa86bSJames Feist             nlohmann::json& pids = configRoot["PidControllers"];
3065b4aa86bSJames Feist             pids["@odata.type"] = "#OemManager.PidControllers";
3075b4aa86bSJames Feist             pids["@odata.id"] =
3085b4aa86bSJames Feist                 "/redfish/v1/Managers/bmc#/Oem/OpenBmc/Fan/PidControllers";
3095b4aa86bSJames Feist 
310b7a08d04SJames Feist             nlohmann::json& stepwise = configRoot["StepwiseControllers"];
311b7a08d04SJames Feist             stepwise["@odata.type"] = "#OemManager.StepwiseControllers";
312b7a08d04SJames Feist             stepwise["@odata.id"] =
313b7a08d04SJames Feist                 "/redfish/v1/Managers/bmc#/Oem/OpenBmc/Fan/StepwiseControllers";
314b7a08d04SJames Feist 
3155b4aa86bSJames Feist             nlohmann::json& zones = configRoot["FanZones"];
3165b4aa86bSJames Feist             zones["@odata.id"] =
3175b4aa86bSJames Feist                 "/redfish/v1/Managers/bmc#/Oem/OpenBmc/Fan/FanZones";
3185b4aa86bSJames Feist             zones["@odata.type"] = "#OemManager.FanZones";
3195b4aa86bSJames Feist             configRoot["@odata.id"] =
3205b4aa86bSJames Feist                 "/redfish/v1/Managers/bmc#/Oem/OpenBmc/Fan";
3215b4aa86bSJames Feist             configRoot["@odata.type"] = "#OemManager.Fan";
32273df0db0SJames Feist             configRoot["Profile@Redfish.AllowableValues"] = supportedProfiles;
32373df0db0SJames Feist 
32473df0db0SJames Feist             if (!currentProfile.empty())
32573df0db0SJames Feist             {
32673df0db0SJames Feist                 configRoot["Profile"] = currentProfile;
32773df0db0SJames Feist             }
32873df0db0SJames Feist             BMCWEB_LOG_ERROR << "profile = " << currentProfile << " !";
3295b4aa86bSJames Feist 
3305b4aa86bSJames Feist             for (const auto& pathPair : managedObj)
3315b4aa86bSJames Feist             {
3325b4aa86bSJames Feist                 for (const auto& intfPair : pathPair.second)
3335b4aa86bSJames Feist                 {
3345b4aa86bSJames Feist                     if (intfPair.first != pidConfigurationIface &&
335b7a08d04SJames Feist                         intfPair.first != pidZoneConfigurationIface &&
336b7a08d04SJames Feist                         intfPair.first != stepwiseConfigurationIface)
3375b4aa86bSJames Feist                     {
3385b4aa86bSJames Feist                         continue;
3395b4aa86bSJames Feist                     }
3405b4aa86bSJames Feist                     auto findName = intfPair.second.find("Name");
3415b4aa86bSJames Feist                     if (findName == intfPair.second.end())
3425b4aa86bSJames Feist                     {
3435b4aa86bSJames Feist                         BMCWEB_LOG_ERROR << "Pid Field missing Name";
344a08b46ccSJason M. Bills                         messages::internalError(asyncResp->res);
3455b4aa86bSJames Feist                         return;
3465b4aa86bSJames Feist                     }
34773df0db0SJames Feist 
3485b4aa86bSJames Feist                     const std::string* namePtr =
349abf2add6SEd Tanous                         std::get_if<std::string>(&findName->second);
3505b4aa86bSJames Feist                     if (namePtr == nullptr)
3515b4aa86bSJames Feist                     {
3525b4aa86bSJames Feist                         BMCWEB_LOG_ERROR << "Pid Name Field illegal";
353b7a08d04SJames Feist                         messages::internalError(asyncResp->res);
3545b4aa86bSJames Feist                         return;
3555b4aa86bSJames Feist                     }
3565b4aa86bSJames Feist                     std::string name = *namePtr;
3575b4aa86bSJames Feist                     dbus::utility::escapePathForDbus(name);
35873df0db0SJames Feist 
35973df0db0SJames Feist                     auto findProfiles = intfPair.second.find("Profiles");
36073df0db0SJames Feist                     if (findProfiles != intfPair.second.end())
36173df0db0SJames Feist                     {
36273df0db0SJames Feist                         const std::vector<std::string>* profiles =
36373df0db0SJames Feist                             std::get_if<std::vector<std::string>>(
36473df0db0SJames Feist                                 &findProfiles->second);
36573df0db0SJames Feist                         if (profiles == nullptr)
36673df0db0SJames Feist                         {
36773df0db0SJames Feist                             BMCWEB_LOG_ERROR << "Pid Profiles Field illegal";
36873df0db0SJames Feist                             messages::internalError(asyncResp->res);
36973df0db0SJames Feist                             return;
37073df0db0SJames Feist                         }
37173df0db0SJames Feist                         if (std::find(profiles->begin(), profiles->end(),
37273df0db0SJames Feist                                       currentProfile) == profiles->end())
37373df0db0SJames Feist                         {
37473df0db0SJames Feist                             BMCWEB_LOG_INFO
37573df0db0SJames Feist                                 << name << " not supported in current profile";
37673df0db0SJames Feist                             continue;
37773df0db0SJames Feist                         }
37873df0db0SJames Feist                     }
379b7a08d04SJames Feist                     nlohmann::json* config = nullptr;
380c33a90ecSJames Feist 
381c33a90ecSJames Feist                     const std::string* classPtr = nullptr;
382c33a90ecSJames Feist                     auto findClass = intfPair.second.find("Class");
383c33a90ecSJames Feist                     if (findClass != intfPair.second.end())
384c33a90ecSJames Feist                     {
385c33a90ecSJames Feist                         classPtr = std::get_if<std::string>(&findClass->second);
386c33a90ecSJames Feist                     }
387c33a90ecSJames Feist 
3885b4aa86bSJames Feist                     if (intfPair.first == pidZoneConfigurationIface)
3895b4aa86bSJames Feist                     {
3905b4aa86bSJames Feist                         std::string chassis;
3915b4aa86bSJames Feist                         if (!dbus::utility::getNthStringFromPath(
3925b4aa86bSJames Feist                                 pathPair.first.str, 5, chassis))
3935b4aa86bSJames Feist                         {
3945b4aa86bSJames Feist                             chassis = "#IllegalValue";
3955b4aa86bSJames Feist                         }
3965b4aa86bSJames Feist                         nlohmann::json& zone = zones[name];
3975b4aa86bSJames Feist                         zone["Chassis"] = {
3985b4aa86bSJames Feist                             {"@odata.id", "/redfish/v1/Chassis/" + chassis}};
3995b4aa86bSJames Feist                         zone["@odata.id"] = "/redfish/v1/Managers/bmc#/Oem/"
4005b4aa86bSJames Feist                                             "OpenBmc/Fan/FanZones/" +
4015b4aa86bSJames Feist                                             name;
4025b4aa86bSJames Feist                         zone["@odata.type"] = "#OemManager.FanZone";
403b7a08d04SJames Feist                         config = &zone;
4045b4aa86bSJames Feist                     }
4055b4aa86bSJames Feist 
406b7a08d04SJames Feist                     else if (intfPair.first == stepwiseConfigurationIface)
4075b4aa86bSJames Feist                     {
408c33a90ecSJames Feist                         if (classPtr == nullptr)
409c33a90ecSJames Feist                         {
410c33a90ecSJames Feist                             BMCWEB_LOG_ERROR << "Pid Class Field illegal";
411c33a90ecSJames Feist                             messages::internalError(asyncResp->res);
412c33a90ecSJames Feist                             return;
413c33a90ecSJames Feist                         }
414c33a90ecSJames Feist 
415b7a08d04SJames Feist                         nlohmann::json& controller = stepwise[name];
416b7a08d04SJames Feist                         config = &controller;
4175b4aa86bSJames Feist 
418b7a08d04SJames Feist                         controller["@odata.id"] =
419b7a08d04SJames Feist                             "/redfish/v1/Managers/bmc#/Oem/"
420b7a08d04SJames Feist                             "OpenBmc/Fan/StepwiseControllers/" +
421271584abSEd Tanous                             name;
422b7a08d04SJames Feist                         controller["@odata.type"] =
423b7a08d04SJames Feist                             "#OemManager.StepwiseController";
424b7a08d04SJames Feist 
425c33a90ecSJames Feist                         controller["Direction"] = *classPtr;
4265b4aa86bSJames Feist                     }
4275b4aa86bSJames Feist 
4285b4aa86bSJames Feist                     // pid and fans are off the same configuration
429b7a08d04SJames Feist                     else if (intfPair.first == pidConfigurationIface)
4305b4aa86bSJames Feist                     {
431c33a90ecSJames Feist 
4325b4aa86bSJames Feist                         if (classPtr == nullptr)
4335b4aa86bSJames Feist                         {
4345b4aa86bSJames Feist                             BMCWEB_LOG_ERROR << "Pid Class Field illegal";
435a08b46ccSJason M. Bills                             messages::internalError(asyncResp->res);
4365b4aa86bSJames Feist                             return;
4375b4aa86bSJames Feist                         }
4385b4aa86bSJames Feist                         bool isFan = *classPtr == "fan";
4395b4aa86bSJames Feist                         nlohmann::json& element =
4405b4aa86bSJames Feist                             isFan ? fans[name] : pids[name];
441b7a08d04SJames Feist                         config = &element;
4425b4aa86bSJames Feist                         if (isFan)
4435b4aa86bSJames Feist                         {
4445b4aa86bSJames Feist                             element["@odata.id"] =
4455b4aa86bSJames Feist                                 "/redfish/v1/Managers/bmc#/Oem/"
4465b4aa86bSJames Feist                                 "OpenBmc/Fan/FanControllers/" +
447271584abSEd Tanous                                 name;
4485b4aa86bSJames Feist                             element["@odata.type"] =
4495b4aa86bSJames Feist                                 "#OemManager.FanController";
4505b4aa86bSJames Feist                         }
4515b4aa86bSJames Feist                         else
4525b4aa86bSJames Feist                         {
4535b4aa86bSJames Feist                             element["@odata.id"] =
4545b4aa86bSJames Feist                                 "/redfish/v1/Managers/bmc#/Oem/"
4555b4aa86bSJames Feist                                 "OpenBmc/Fan/PidControllers/" +
456271584abSEd Tanous                                 name;
4575b4aa86bSJames Feist                             element["@odata.type"] =
4585b4aa86bSJames Feist                                 "#OemManager.PidController";
4595b4aa86bSJames Feist                         }
460b7a08d04SJames Feist                     }
461b7a08d04SJames Feist                     else
462b7a08d04SJames Feist                     {
463b7a08d04SJames Feist                         BMCWEB_LOG_ERROR << "Unexpected configuration";
464b7a08d04SJames Feist                         messages::internalError(asyncResp->res);
465b7a08d04SJames Feist                         return;
466b7a08d04SJames Feist                     }
467b7a08d04SJames Feist 
468b7a08d04SJames Feist                     // used for making maps out of 2 vectors
469b7a08d04SJames Feist                     const std::vector<double>* keys = nullptr;
470b7a08d04SJames Feist                     const std::vector<double>* values = nullptr;
471b7a08d04SJames Feist 
472b7a08d04SJames Feist                     for (const auto& propertyPair : intfPair.second)
473b7a08d04SJames Feist                     {
474b7a08d04SJames Feist                         if (propertyPair.first == "Type" ||
475b7a08d04SJames Feist                             propertyPair.first == "Class" ||
476b7a08d04SJames Feist                             propertyPair.first == "Name")
477b7a08d04SJames Feist                         {
478b7a08d04SJames Feist                             continue;
479b7a08d04SJames Feist                         }
480b7a08d04SJames Feist 
481b7a08d04SJames Feist                         // zones
482b7a08d04SJames Feist                         if (intfPair.first == pidZoneConfigurationIface)
483b7a08d04SJames Feist                         {
484b7a08d04SJames Feist                             const double* ptr =
485abf2add6SEd Tanous                                 std::get_if<double>(&propertyPair.second);
486b7a08d04SJames Feist                             if (ptr == nullptr)
487b7a08d04SJames Feist                             {
488b7a08d04SJames Feist                                 BMCWEB_LOG_ERROR << "Field Illegal "
489b7a08d04SJames Feist                                                  << propertyPair.first;
490b7a08d04SJames Feist                                 messages::internalError(asyncResp->res);
491b7a08d04SJames Feist                                 return;
492b7a08d04SJames Feist                             }
493b7a08d04SJames Feist                             (*config)[propertyPair.first] = *ptr;
494b7a08d04SJames Feist                         }
495b7a08d04SJames Feist 
496b7a08d04SJames Feist                         if (intfPair.first == stepwiseConfigurationIface)
497b7a08d04SJames Feist                         {
498b7a08d04SJames Feist                             if (propertyPair.first == "Reading" ||
499b7a08d04SJames Feist                                 propertyPair.first == "Output")
500b7a08d04SJames Feist                             {
501b7a08d04SJames Feist                                 const std::vector<double>* ptr =
502abf2add6SEd Tanous                                     std::get_if<std::vector<double>>(
503b7a08d04SJames Feist                                         &propertyPair.second);
504b7a08d04SJames Feist 
505b7a08d04SJames Feist                                 if (ptr == nullptr)
506b7a08d04SJames Feist                                 {
507b7a08d04SJames Feist                                     BMCWEB_LOG_ERROR << "Field Illegal "
508b7a08d04SJames Feist                                                      << propertyPair.first;
509b7a08d04SJames Feist                                     messages::internalError(asyncResp->res);
510b7a08d04SJames Feist                                     return;
511b7a08d04SJames Feist                                 }
512b7a08d04SJames Feist 
513b7a08d04SJames Feist                                 if (propertyPair.first == "Reading")
514b7a08d04SJames Feist                                 {
515b7a08d04SJames Feist                                     keys = ptr;
516b7a08d04SJames Feist                                 }
517b7a08d04SJames Feist                                 else
518b7a08d04SJames Feist                                 {
519b7a08d04SJames Feist                                     values = ptr;
520b7a08d04SJames Feist                                 }
521b7a08d04SJames Feist                                 if (keys && values)
522b7a08d04SJames Feist                                 {
523b7a08d04SJames Feist                                     if (keys->size() != values->size())
524b7a08d04SJames Feist                                     {
525b7a08d04SJames Feist                                         BMCWEB_LOG_ERROR
526b7a08d04SJames Feist                                             << "Reading and Output size don't "
527b7a08d04SJames Feist                                                "match ";
528b7a08d04SJames Feist                                         messages::internalError(asyncResp->res);
529b7a08d04SJames Feist                                         return;
530b7a08d04SJames Feist                                     }
531b7a08d04SJames Feist                                     nlohmann::json& steps = (*config)["Steps"];
532b7a08d04SJames Feist                                     steps = nlohmann::json::array();
533b7a08d04SJames Feist                                     for (size_t ii = 0; ii < keys->size(); ii++)
534b7a08d04SJames Feist                                     {
535b7a08d04SJames Feist                                         steps.push_back(
536b7a08d04SJames Feist                                             {{"Target", (*keys)[ii]},
537b7a08d04SJames Feist                                              {"Output", (*values)[ii]}});
538b7a08d04SJames Feist                                     }
539b7a08d04SJames Feist                                 }
540b7a08d04SJames Feist                             }
541b7a08d04SJames Feist                             if (propertyPair.first == "NegativeHysteresis" ||
542b7a08d04SJames Feist                                 propertyPair.first == "PositiveHysteresis")
543b7a08d04SJames Feist                             {
544b7a08d04SJames Feist                                 const double* ptr =
545abf2add6SEd Tanous                                     std::get_if<double>(&propertyPair.second);
546b7a08d04SJames Feist                                 if (ptr == nullptr)
547b7a08d04SJames Feist                                 {
548b7a08d04SJames Feist                                     BMCWEB_LOG_ERROR << "Field Illegal "
549b7a08d04SJames Feist                                                      << propertyPair.first;
550b7a08d04SJames Feist                                     messages::internalError(asyncResp->res);
551b7a08d04SJames Feist                                     return;
552b7a08d04SJames Feist                                 }
553b7a08d04SJames Feist                                 (*config)[propertyPair.first] = *ptr;
554b7a08d04SJames Feist                             }
555b7a08d04SJames Feist                         }
556b7a08d04SJames Feist 
557b7a08d04SJames Feist                         // pid and fans are off the same configuration
558b7a08d04SJames Feist                         if (intfPair.first == pidConfigurationIface ||
559b7a08d04SJames Feist                             intfPair.first == stepwiseConfigurationIface)
560b7a08d04SJames Feist                         {
5615b4aa86bSJames Feist 
5625b4aa86bSJames Feist                             if (propertyPair.first == "Zones")
5635b4aa86bSJames Feist                             {
5645b4aa86bSJames Feist                                 const std::vector<std::string>* inputs =
565abf2add6SEd Tanous                                     std::get_if<std::vector<std::string>>(
5661b6b96c5SEd Tanous                                         &propertyPair.second);
5675b4aa86bSJames Feist 
5685b4aa86bSJames Feist                                 if (inputs == nullptr)
5695b4aa86bSJames Feist                                 {
5705b4aa86bSJames Feist                                     BMCWEB_LOG_ERROR
5715b4aa86bSJames Feist                                         << "Zones Pid Field Illegal";
572a08b46ccSJason M. Bills                                     messages::internalError(asyncResp->res);
5735b4aa86bSJames Feist                                     return;
5745b4aa86bSJames Feist                                 }
575b7a08d04SJames Feist                                 auto& data = (*config)[propertyPair.first];
5765b4aa86bSJames Feist                                 data = nlohmann::json::array();
5775b4aa86bSJames Feist                                 for (std::string itemCopy : *inputs)
5785b4aa86bSJames Feist                                 {
5795b4aa86bSJames Feist                                     dbus::utility::escapePathForDbus(itemCopy);
5805b4aa86bSJames Feist                                     data.push_back(
5815b4aa86bSJames Feist                                         {{"@odata.id",
5825b4aa86bSJames Feist                                           "/redfish/v1/Managers/bmc#/Oem/"
5835b4aa86bSJames Feist                                           "OpenBmc/Fan/FanZones/" +
5845b4aa86bSJames Feist                                               itemCopy}});
5855b4aa86bSJames Feist                                 }
5865b4aa86bSJames Feist                             }
5875b4aa86bSJames Feist                             // todo(james): may never happen, but this
5885b4aa86bSJames Feist                             // assumes configuration data referenced in the
5895b4aa86bSJames Feist                             // PID config is provided by the same daemon, we
5905b4aa86bSJames Feist                             // could add another loop to cover all cases,
5915b4aa86bSJames Feist                             // but I'm okay kicking this can down the road a
5925b4aa86bSJames Feist                             // bit
5935b4aa86bSJames Feist 
5945b4aa86bSJames Feist                             else if (propertyPair.first == "Inputs" ||
5955b4aa86bSJames Feist                                      propertyPair.first == "Outputs")
5965b4aa86bSJames Feist                             {
597b7a08d04SJames Feist                                 auto& data = (*config)[propertyPair.first];
5985b4aa86bSJames Feist                                 const std::vector<std::string>* inputs =
599abf2add6SEd Tanous                                     std::get_if<std::vector<std::string>>(
6001b6b96c5SEd Tanous                                         &propertyPair.second);
6015b4aa86bSJames Feist 
6025b4aa86bSJames Feist                                 if (inputs == nullptr)
6035b4aa86bSJames Feist                                 {
6045b4aa86bSJames Feist                                     BMCWEB_LOG_ERROR << "Field Illegal "
6055b4aa86bSJames Feist                                                      << propertyPair.first;
606f12894f8SJason M. Bills                                     messages::internalError(asyncResp->res);
6075b4aa86bSJames Feist                                     return;
6085b4aa86bSJames Feist                                 }
6095b4aa86bSJames Feist                                 data = *inputs;
610b943aaefSJames Feist                             }
611b943aaefSJames Feist                             else if (propertyPair.first == "SetPointOffset")
612b943aaefSJames Feist                             {
613b943aaefSJames Feist                                 const std::string* ptr =
614b943aaefSJames Feist                                     std::get_if<std::string>(
615b943aaefSJames Feist                                         &propertyPair.second);
616b943aaefSJames Feist 
617b943aaefSJames Feist                                 if (ptr == nullptr)
618b943aaefSJames Feist                                 {
619b943aaefSJames Feist                                     BMCWEB_LOG_ERROR << "Field Illegal "
620b943aaefSJames Feist                                                      << propertyPair.first;
621b943aaefSJames Feist                                     messages::internalError(asyncResp->res);
622b943aaefSJames Feist                                     return;
623b943aaefSJames Feist                                 }
624b943aaefSJames Feist                                 // translate from dbus to redfish
625b943aaefSJames Feist                                 if (*ptr == "WarningHigh")
626b943aaefSJames Feist                                 {
627b943aaefSJames Feist                                     (*config)["SetPointOffset"] =
628b943aaefSJames Feist                                         "UpperThresholdNonCritical";
629b943aaefSJames Feist                                 }
630b943aaefSJames Feist                                 else if (*ptr == "WarningLow")
631b943aaefSJames Feist                                 {
632b943aaefSJames Feist                                     (*config)["SetPointOffset"] =
633b943aaefSJames Feist                                         "LowerThresholdNonCritical";
634b943aaefSJames Feist                                 }
635b943aaefSJames Feist                                 else if (*ptr == "CriticalHigh")
636b943aaefSJames Feist                                 {
637b943aaefSJames Feist                                     (*config)["SetPointOffset"] =
638b943aaefSJames Feist                                         "UpperThresholdCritical";
639b943aaefSJames Feist                                 }
640b943aaefSJames Feist                                 else if (*ptr == "CriticalLow")
641b943aaefSJames Feist                                 {
642b943aaefSJames Feist                                     (*config)["SetPointOffset"] =
643b943aaefSJames Feist                                         "LowerThresholdCritical";
644b943aaefSJames Feist                                 }
645b943aaefSJames Feist                                 else
646b943aaefSJames Feist                                 {
647b943aaefSJames Feist                                     BMCWEB_LOG_ERROR << "Value Illegal "
648b943aaefSJames Feist                                                      << *ptr;
649b943aaefSJames Feist                                     messages::internalError(asyncResp->res);
650b943aaefSJames Feist                                     return;
651b943aaefSJames Feist                                 }
652b943aaefSJames Feist                             }
653b943aaefSJames Feist                             // doubles
6545b4aa86bSJames Feist                             else if (propertyPair.first ==
6555b4aa86bSJames Feist                                          "FFGainCoefficient" ||
6565b4aa86bSJames Feist                                      propertyPair.first == "FFOffCoefficient" ||
6575b4aa86bSJames Feist                                      propertyPair.first == "ICoefficient" ||
6585b4aa86bSJames Feist                                      propertyPair.first == "ILimitMax" ||
6595b4aa86bSJames Feist                                      propertyPair.first == "ILimitMin" ||
660aad1a257SJames Feist                                      propertyPair.first ==
661aad1a257SJames Feist                                          "PositiveHysteresis" ||
662aad1a257SJames Feist                                      propertyPair.first ==
663aad1a257SJames Feist                                          "NegativeHysteresis" ||
6645b4aa86bSJames Feist                                      propertyPair.first == "OutLimitMax" ||
6655b4aa86bSJames Feist                                      propertyPair.first == "OutLimitMin" ||
6665b4aa86bSJames Feist                                      propertyPair.first == "PCoefficient" ||
6677625cb81SJames Feist                                      propertyPair.first == "SetPoint" ||
6685b4aa86bSJames Feist                                      propertyPair.first == "SlewNeg" ||
6695b4aa86bSJames Feist                                      propertyPair.first == "SlewPos")
6705b4aa86bSJames Feist                             {
6715b4aa86bSJames Feist                                 const double* ptr =
672abf2add6SEd Tanous                                     std::get_if<double>(&propertyPair.second);
6735b4aa86bSJames Feist                                 if (ptr == nullptr)
6745b4aa86bSJames Feist                                 {
6755b4aa86bSJames Feist                                     BMCWEB_LOG_ERROR << "Field Illegal "
6765b4aa86bSJames Feist                                                      << propertyPair.first;
677f12894f8SJason M. Bills                                     messages::internalError(asyncResp->res);
6785b4aa86bSJames Feist                                     return;
6795b4aa86bSJames Feist                                 }
680b7a08d04SJames Feist                                 (*config)[propertyPair.first] = *ptr;
6815b4aa86bSJames Feist                             }
6825b4aa86bSJames Feist                         }
6835b4aa86bSJames Feist                     }
6845b4aa86bSJames Feist                 }
6855b4aa86bSJames Feist             }
6865b4aa86bSJames Feist         },
6875b4aa86bSJames Feist         connection, path, objectManagerIface, "GetManagedObjects");
6885b4aa86bSJames Feist }
689ca537928SJennifer Lee 
69083ff9ab6SJames Feist enum class CreatePIDRet
69183ff9ab6SJames Feist {
69283ff9ab6SJames Feist     fail,
69383ff9ab6SJames Feist     del,
69483ff9ab6SJames Feist     patch
69583ff9ab6SJames Feist };
69683ff9ab6SJames Feist 
69723a21a1cSEd Tanous inline bool getZonesFromJsonReq(const std::shared_ptr<AsyncResp>& response,
6985f2caaefSJames Feist                                 std::vector<nlohmann::json>& config,
6995f2caaefSJames Feist                                 std::vector<std::string>& zones)
7005f2caaefSJames Feist {
701b6baeaa4SJames Feist     if (config.empty())
702b6baeaa4SJames Feist     {
703b6baeaa4SJames Feist         BMCWEB_LOG_ERROR << "Empty Zones";
704b6baeaa4SJames Feist         messages::propertyValueFormatError(response->res,
705b6baeaa4SJames Feist                                            nlohmann::json::array(), "Zones");
706b6baeaa4SJames Feist         return false;
707b6baeaa4SJames Feist     }
7085f2caaefSJames Feist     for (auto& odata : config)
7095f2caaefSJames Feist     {
7105f2caaefSJames Feist         std::string path;
7115f2caaefSJames Feist         if (!redfish::json_util::readJson(odata, response->res, "@odata.id",
7125f2caaefSJames Feist                                           path))
7135f2caaefSJames Feist         {
7145f2caaefSJames Feist             return false;
7155f2caaefSJames Feist         }
7165f2caaefSJames Feist         std::string input;
71761adbda3SJames Feist 
71861adbda3SJames Feist         // 8 below comes from
71961adbda3SJames Feist         // /redfish/v1/Managers/bmc#/Oem/OpenBmc/Fan/FanZones/Left
72061adbda3SJames Feist         //     0    1     2      3    4    5      6     7      8
72161adbda3SJames Feist         if (!dbus::utility::getNthStringFromPath(path, 8, input))
7225f2caaefSJames Feist         {
7235f2caaefSJames Feist             BMCWEB_LOG_ERROR << "Got invalid path " << path;
7245f2caaefSJames Feist             BMCWEB_LOG_ERROR << "Illegal Type Zones";
7255f2caaefSJames Feist             messages::propertyValueFormatError(response->res, odata.dump(),
7265f2caaefSJames Feist                                                "Zones");
7275f2caaefSJames Feist             return false;
7285f2caaefSJames Feist         }
7295f2caaefSJames Feist         boost::replace_all(input, "_", " ");
7305f2caaefSJames Feist         zones.emplace_back(std::move(input));
7315f2caaefSJames Feist     }
7325f2caaefSJames Feist     return true;
7335f2caaefSJames Feist }
7345f2caaefSJames Feist 
73523a21a1cSEd Tanous inline const dbus::utility::ManagedItem*
73673df0db0SJames Feist     findChassis(const dbus::utility::ManagedObjectType& managedObj,
737b6baeaa4SJames Feist                 const std::string& value, std::string& chassis)
738b6baeaa4SJames Feist {
739b6baeaa4SJames Feist     BMCWEB_LOG_DEBUG << "Find Chassis: " << value << "\n";
740b6baeaa4SJames Feist 
741b6baeaa4SJames Feist     std::string escaped = boost::replace_all_copy(value, " ", "_");
742b6baeaa4SJames Feist     escaped = "/" + escaped;
743b6baeaa4SJames Feist     auto it = std::find_if(
744b6baeaa4SJames Feist         managedObj.begin(), managedObj.end(), [&escaped](const auto& obj) {
745b6baeaa4SJames Feist             if (boost::algorithm::ends_with(obj.first.str, escaped))
746b6baeaa4SJames Feist             {
747b6baeaa4SJames Feist                 BMCWEB_LOG_DEBUG << "Matched " << obj.first.str << "\n";
748b6baeaa4SJames Feist                 return true;
749b6baeaa4SJames Feist             }
750b6baeaa4SJames Feist             return false;
751b6baeaa4SJames Feist         });
752b6baeaa4SJames Feist 
753b6baeaa4SJames Feist     if (it == managedObj.end())
754b6baeaa4SJames Feist     {
75573df0db0SJames Feist         return nullptr;
756b6baeaa4SJames Feist     }
757b6baeaa4SJames Feist     // 5 comes from <chassis-name> being the 5th element
758b6baeaa4SJames Feist     // /xyz/openbmc_project/inventory/system/chassis/<chassis-name>
75973df0db0SJames Feist     if (dbus::utility::getNthStringFromPath(it->first.str, 5, chassis))
76073df0db0SJames Feist     {
76173df0db0SJames Feist         return &(*it);
76273df0db0SJames Feist     }
76373df0db0SJames Feist 
76473df0db0SJames Feist     return nullptr;
765b6baeaa4SJames Feist }
766b6baeaa4SJames Feist 
76723a21a1cSEd Tanous inline CreatePIDRet createPidInterface(
76883ff9ab6SJames Feist     const std::shared_ptr<AsyncResp>& response, const std::string& type,
769b6baeaa4SJames Feist     nlohmann::json::iterator it, const std::string& path,
77083ff9ab6SJames Feist     const dbus::utility::ManagedObjectType& managedObj, bool createNewObject,
77183ff9ab6SJames Feist     boost::container::flat_map<std::string, dbus::utility::DbusVariantType>&
77283ff9ab6SJames Feist         output,
77373df0db0SJames Feist     std::string& chassis, const std::string& profile)
77483ff9ab6SJames Feist {
77583ff9ab6SJames Feist 
7765f2caaefSJames Feist     // common deleter
777b6baeaa4SJames Feist     if (it.value() == nullptr)
7785f2caaefSJames Feist     {
7795f2caaefSJames Feist         std::string iface;
7805f2caaefSJames Feist         if (type == "PidControllers" || type == "FanControllers")
7815f2caaefSJames Feist         {
7825f2caaefSJames Feist             iface = pidConfigurationIface;
7835f2caaefSJames Feist         }
7845f2caaefSJames Feist         else if (type == "FanZones")
7855f2caaefSJames Feist         {
7865f2caaefSJames Feist             iface = pidZoneConfigurationIface;
7875f2caaefSJames Feist         }
7885f2caaefSJames Feist         else if (type == "StepwiseControllers")
7895f2caaefSJames Feist         {
7905f2caaefSJames Feist             iface = stepwiseConfigurationIface;
7915f2caaefSJames Feist         }
7925f2caaefSJames Feist         else
7935f2caaefSJames Feist         {
7945f2caaefSJames Feist             BMCWEB_LOG_ERROR << "Line:" << __LINE__ << ", Illegal Type "
7955f2caaefSJames Feist                              << type;
7965f2caaefSJames Feist             messages::propertyUnknown(response->res, type);
7975f2caaefSJames Feist             return CreatePIDRet::fail;
7985f2caaefSJames Feist         }
7996ee7f774SJames Feist 
8006ee7f774SJames Feist         BMCWEB_LOG_DEBUG << "del " << path << " " << iface << "\n";
8015f2caaefSJames Feist         // delete interface
8025f2caaefSJames Feist         crow::connections::systemBus->async_method_call(
8035f2caaefSJames Feist             [response, path](const boost::system::error_code ec) {
8045f2caaefSJames Feist                 if (ec)
8055f2caaefSJames Feist                 {
8065f2caaefSJames Feist                     BMCWEB_LOG_ERROR << "Error patching " << path << ": " << ec;
8075f2caaefSJames Feist                     messages::internalError(response->res);
808b6baeaa4SJames Feist                     return;
8095f2caaefSJames Feist                 }
810b6baeaa4SJames Feist                 messages::success(response->res);
8115f2caaefSJames Feist             },
8125f2caaefSJames Feist             "xyz.openbmc_project.EntityManager", path, iface, "Delete");
8135f2caaefSJames Feist         return CreatePIDRet::del;
8145f2caaefSJames Feist     }
8155f2caaefSJames Feist 
81673df0db0SJames Feist     const dbus::utility::ManagedItem* managedItem = nullptr;
817b6baeaa4SJames Feist     if (!createNewObject)
818b6baeaa4SJames Feist     {
819b6baeaa4SJames Feist         // if we aren't creating a new object, we should be able to find it on
820b6baeaa4SJames Feist         // d-bus
82173df0db0SJames Feist         managedItem = findChassis(managedObj, it.key(), chassis);
82273df0db0SJames Feist         if (managedItem == nullptr)
823b6baeaa4SJames Feist         {
824b6baeaa4SJames Feist             BMCWEB_LOG_ERROR << "Failed to get chassis from config patch";
825b6baeaa4SJames Feist             messages::invalidObject(response->res, it.key());
826b6baeaa4SJames Feist             return CreatePIDRet::fail;
827b6baeaa4SJames Feist         }
828b6baeaa4SJames Feist     }
829b6baeaa4SJames Feist 
83073df0db0SJames Feist     if (profile.size() &&
83173df0db0SJames Feist         (type == "PidControllers" || type == "FanControllers" ||
83273df0db0SJames Feist          type == "StepwiseControllers"))
83373df0db0SJames Feist     {
83473df0db0SJames Feist         if (managedItem == nullptr)
83573df0db0SJames Feist         {
83673df0db0SJames Feist             output["Profiles"] = std::vector<std::string>{profile};
83773df0db0SJames Feist         }
83873df0db0SJames Feist         else
83973df0db0SJames Feist         {
84073df0db0SJames Feist             std::string interface;
84173df0db0SJames Feist             if (type == "StepwiseControllers")
84273df0db0SJames Feist             {
84373df0db0SJames Feist                 interface = stepwiseConfigurationIface;
84473df0db0SJames Feist             }
84573df0db0SJames Feist             else
84673df0db0SJames Feist             {
84773df0db0SJames Feist                 interface = pidConfigurationIface;
84873df0db0SJames Feist             }
84973df0db0SJames Feist             auto findConfig = managedItem->second.find(interface);
85073df0db0SJames Feist             if (findConfig == managedItem->second.end())
85173df0db0SJames Feist             {
85273df0db0SJames Feist                 BMCWEB_LOG_ERROR
85373df0db0SJames Feist                     << "Failed to find interface in managed object";
85473df0db0SJames Feist                 messages::internalError(response->res);
85573df0db0SJames Feist                 return CreatePIDRet::fail;
85673df0db0SJames Feist             }
85773df0db0SJames Feist             auto findProfiles = findConfig->second.find("Profiles");
85873df0db0SJames Feist             if (findProfiles != findConfig->second.end())
85973df0db0SJames Feist             {
86073df0db0SJames Feist                 const std::vector<std::string>* curProfiles =
86173df0db0SJames Feist                     std::get_if<std::vector<std::string>>(
86273df0db0SJames Feist                         &(findProfiles->second));
86373df0db0SJames Feist                 if (curProfiles == nullptr)
86473df0db0SJames Feist                 {
86573df0db0SJames Feist                     BMCWEB_LOG_ERROR << "Illegal profiles in managed object";
86673df0db0SJames Feist                     messages::internalError(response->res);
86773df0db0SJames Feist                     return CreatePIDRet::fail;
86873df0db0SJames Feist                 }
86973df0db0SJames Feist                 if (std::find(curProfiles->begin(), curProfiles->end(),
87073df0db0SJames Feist                               profile) == curProfiles->end())
87173df0db0SJames Feist                 {
87273df0db0SJames Feist                     std::vector<std::string> newProfiles = *curProfiles;
87373df0db0SJames Feist                     newProfiles.push_back(profile);
87473df0db0SJames Feist                     output["Profiles"] = newProfiles;
87573df0db0SJames Feist                 }
87673df0db0SJames Feist             }
87773df0db0SJames Feist         }
87873df0db0SJames Feist     }
87973df0db0SJames Feist 
88083ff9ab6SJames Feist     if (type == "PidControllers" || type == "FanControllers")
88183ff9ab6SJames Feist     {
88283ff9ab6SJames Feist         if (createNewObject)
88383ff9ab6SJames Feist         {
88483ff9ab6SJames Feist             output["Class"] = type == "PidControllers" ? std::string("temp")
88583ff9ab6SJames Feist                                                        : std::string("fan");
88683ff9ab6SJames Feist             output["Type"] = std::string("Pid");
88783ff9ab6SJames Feist         }
8885f2caaefSJames Feist 
8895f2caaefSJames Feist         std::optional<std::vector<nlohmann::json>> zones;
8905f2caaefSJames Feist         std::optional<std::vector<std::string>> inputs;
8915f2caaefSJames Feist         std::optional<std::vector<std::string>> outputs;
8925f2caaefSJames Feist         std::map<std::string, std::optional<double>> doubles;
893b943aaefSJames Feist         std::optional<std::string> setpointOffset;
8945f2caaefSJames Feist         if (!redfish::json_util::readJson(
895b6baeaa4SJames Feist                 it.value(), response->res, "Inputs", inputs, "Outputs", outputs,
8965f2caaefSJames Feist                 "Zones", zones, "FFGainCoefficient",
8975f2caaefSJames Feist                 doubles["FFGainCoefficient"], "FFOffCoefficient",
8985f2caaefSJames Feist                 doubles["FFOffCoefficient"], "ICoefficient",
8995f2caaefSJames Feist                 doubles["ICoefficient"], "ILimitMax", doubles["ILimitMax"],
9005f2caaefSJames Feist                 "ILimitMin", doubles["ILimitMin"], "OutLimitMax",
9015f2caaefSJames Feist                 doubles["OutLimitMax"], "OutLimitMin", doubles["OutLimitMin"],
9025f2caaefSJames Feist                 "PCoefficient", doubles["PCoefficient"], "SetPoint",
903b943aaefSJames Feist                 doubles["SetPoint"], "SetPointOffset", setpointOffset,
904b943aaefSJames Feist                 "SlewNeg", doubles["SlewNeg"], "SlewPos", doubles["SlewPos"],
905b943aaefSJames Feist                 "PositiveHysteresis", doubles["PositiveHysteresis"],
906b943aaefSJames Feist                 "NegativeHysteresis", doubles["NegativeHysteresis"]))
90783ff9ab6SJames Feist         {
9085f2caaefSJames Feist             BMCWEB_LOG_ERROR << "Line:" << __LINE__ << ", Illegal Property "
909b6baeaa4SJames Feist                              << it.value().dump();
9105f2caaefSJames Feist             return CreatePIDRet::fail;
91183ff9ab6SJames Feist         }
9125f2caaefSJames Feist         if (zones)
9135f2caaefSJames Feist         {
9145f2caaefSJames Feist             std::vector<std::string> zonesStr;
9155f2caaefSJames Feist             if (!getZonesFromJsonReq(response, *zones, zonesStr))
9165f2caaefSJames Feist             {
9175f2caaefSJames Feist                 BMCWEB_LOG_ERROR << "Line:" << __LINE__ << ", Illegal Zones";
9185f2caaefSJames Feist                 return CreatePIDRet::fail;
9195f2caaefSJames Feist             }
920b6baeaa4SJames Feist             if (chassis.empty() &&
921b6baeaa4SJames Feist                 !findChassis(managedObj, zonesStr[0], chassis))
922b6baeaa4SJames Feist             {
923b6baeaa4SJames Feist                 BMCWEB_LOG_ERROR << "Failed to get chassis from config patch";
924b6baeaa4SJames Feist                 messages::invalidObject(response->res, it.key());
925b6baeaa4SJames Feist                 return CreatePIDRet::fail;
926b6baeaa4SJames Feist             }
927b6baeaa4SJames Feist 
9285f2caaefSJames Feist             output["Zones"] = std::move(zonesStr);
9295f2caaefSJames Feist         }
9305f2caaefSJames Feist         if (inputs || outputs)
9315f2caaefSJames Feist         {
9325f2caaefSJames Feist             std::array<std::optional<std::vector<std::string>>*, 2> containers =
9335f2caaefSJames Feist                 {&inputs, &outputs};
9345f2caaefSJames Feist             size_t index = 0;
9355f2caaefSJames Feist             for (const auto& containerPtr : containers)
9365f2caaefSJames Feist             {
9375f2caaefSJames Feist                 std::optional<std::vector<std::string>>& container =
9385f2caaefSJames Feist                     *containerPtr;
9395f2caaefSJames Feist                 if (!container)
9405f2caaefSJames Feist                 {
9415f2caaefSJames Feist                     index++;
9425f2caaefSJames Feist                     continue;
94383ff9ab6SJames Feist                 }
94483ff9ab6SJames Feist 
9455f2caaefSJames Feist                 for (std::string& value : *container)
94683ff9ab6SJames Feist                 {
9475f2caaefSJames Feist                     boost::replace_all(value, "_", " ");
94883ff9ab6SJames Feist                 }
9495f2caaefSJames Feist                 std::string key;
9505f2caaefSJames Feist                 if (index == 0)
9515f2caaefSJames Feist                 {
9525f2caaefSJames Feist                     key = "Inputs";
9535f2caaefSJames Feist                 }
9545f2caaefSJames Feist                 else
9555f2caaefSJames Feist                 {
9565f2caaefSJames Feist                     key = "Outputs";
9575f2caaefSJames Feist                 }
9585f2caaefSJames Feist                 output[key] = *container;
9595f2caaefSJames Feist                 index++;
9605f2caaefSJames Feist             }
96183ff9ab6SJames Feist         }
96283ff9ab6SJames Feist 
963b943aaefSJames Feist         if (setpointOffset)
964b943aaefSJames Feist         {
965b943aaefSJames Feist             // translate between redfish and dbus names
966b943aaefSJames Feist             if (*setpointOffset == "UpperThresholdNonCritical")
967b943aaefSJames Feist             {
968b943aaefSJames Feist                 output["SetPointOffset"] = std::string("WarningLow");
969b943aaefSJames Feist             }
970b943aaefSJames Feist             else if (*setpointOffset == "LowerThresholdNonCritical")
971b943aaefSJames Feist             {
972b943aaefSJames Feist                 output["SetPointOffset"] = std::string("WarningHigh");
973b943aaefSJames Feist             }
974b943aaefSJames Feist             else if (*setpointOffset == "LowerThresholdCritical")
975b943aaefSJames Feist             {
976b943aaefSJames Feist                 output["SetPointOffset"] = std::string("CriticalLow");
977b943aaefSJames Feist             }
978b943aaefSJames Feist             else if (*setpointOffset == "UpperThresholdCritical")
979b943aaefSJames Feist             {
980b943aaefSJames Feist                 output["SetPointOffset"] = std::string("CriticalHigh");
981b943aaefSJames Feist             }
982b943aaefSJames Feist             else
983b943aaefSJames Feist             {
984b943aaefSJames Feist                 BMCWEB_LOG_ERROR << "Invalid setpointoffset "
985b943aaefSJames Feist                                  << *setpointOffset;
986b943aaefSJames Feist                 messages::invalidObject(response->res, it.key());
987b943aaefSJames Feist                 return CreatePIDRet::fail;
988b943aaefSJames Feist             }
989b943aaefSJames Feist         }
990b943aaefSJames Feist 
99183ff9ab6SJames Feist         // doubles
9925f2caaefSJames Feist         for (const auto& pairs : doubles)
99383ff9ab6SJames Feist         {
9945f2caaefSJames Feist             if (!pairs.second)
99583ff9ab6SJames Feist             {
9965f2caaefSJames Feist                 continue;
99783ff9ab6SJames Feist             }
9985f2caaefSJames Feist             BMCWEB_LOG_DEBUG << pairs.first << " = " << *pairs.second;
9995f2caaefSJames Feist             output[pairs.first] = *(pairs.second);
10005f2caaefSJames Feist         }
100183ff9ab6SJames Feist     }
100283ff9ab6SJames Feist 
100383ff9ab6SJames Feist     else if (type == "FanZones")
100483ff9ab6SJames Feist     {
100583ff9ab6SJames Feist         output["Type"] = std::string("Pid.Zone");
100683ff9ab6SJames Feist 
10075f2caaefSJames Feist         std::optional<nlohmann::json> chassisContainer;
10085f2caaefSJames Feist         std::optional<double> failSafePercent;
1009d3ec07f8SJames Feist         std::optional<double> minThermalOutput;
1010b6baeaa4SJames Feist         if (!redfish::json_util::readJson(it.value(), response->res, "Chassis",
10115f2caaefSJames Feist                                           chassisContainer, "FailSafePercent",
1012d3ec07f8SJames Feist                                           failSafePercent, "MinThermalOutput",
1013d3ec07f8SJames Feist                                           minThermalOutput))
101483ff9ab6SJames Feist         {
10155f2caaefSJames Feist             BMCWEB_LOG_ERROR << "Line:" << __LINE__ << ", Illegal Property "
1016b6baeaa4SJames Feist                              << it.value().dump();
101783ff9ab6SJames Feist             return CreatePIDRet::fail;
101883ff9ab6SJames Feist         }
10195f2caaefSJames Feist 
10205f2caaefSJames Feist         if (chassisContainer)
102183ff9ab6SJames Feist         {
10225f2caaefSJames Feist 
10235f2caaefSJames Feist             std::string chassisId;
10245f2caaefSJames Feist             if (!redfish::json_util::readJson(*chassisContainer, response->res,
10255f2caaefSJames Feist                                               "@odata.id", chassisId))
10265f2caaefSJames Feist             {
10275f2caaefSJames Feist                 BMCWEB_LOG_ERROR << "Line:" << __LINE__ << ", Illegal Property "
10285f2caaefSJames Feist                                  << chassisContainer->dump();
102983ff9ab6SJames Feist                 return CreatePIDRet::fail;
103083ff9ab6SJames Feist             }
103183ff9ab6SJames Feist 
1032717794d5SAppaRao Puli             // /redfish/v1/chassis/chassis_name/
10335f2caaefSJames Feist             if (!dbus::utility::getNthStringFromPath(chassisId, 3, chassis))
103483ff9ab6SJames Feist             {
10355f2caaefSJames Feist                 BMCWEB_LOG_ERROR << "Got invalid path " << chassisId;
10365f2caaefSJames Feist                 messages::invalidObject(response->res, chassisId);
103783ff9ab6SJames Feist                 return CreatePIDRet::fail;
103883ff9ab6SJames Feist             }
103983ff9ab6SJames Feist         }
1040d3ec07f8SJames Feist         if (minThermalOutput)
104183ff9ab6SJames Feist         {
1042d3ec07f8SJames Feist             output["MinThermalOutput"] = *minThermalOutput;
10435f2caaefSJames Feist         }
10445f2caaefSJames Feist         if (failSafePercent)
104583ff9ab6SJames Feist         {
10465f2caaefSJames Feist             output["FailSafePercent"] = *failSafePercent;
10475f2caaefSJames Feist         }
10485f2caaefSJames Feist     }
10495f2caaefSJames Feist     else if (type == "StepwiseControllers")
10505f2caaefSJames Feist     {
10515f2caaefSJames Feist         output["Type"] = std::string("Stepwise");
10525f2caaefSJames Feist 
10535f2caaefSJames Feist         std::optional<std::vector<nlohmann::json>> zones;
10545f2caaefSJames Feist         std::optional<std::vector<nlohmann::json>> steps;
10555f2caaefSJames Feist         std::optional<std::vector<std::string>> inputs;
10565f2caaefSJames Feist         std::optional<double> positiveHysteresis;
10575f2caaefSJames Feist         std::optional<double> negativeHysteresis;
1058c33a90ecSJames Feist         std::optional<std::string> direction; // upper clipping curve vs lower
10595f2caaefSJames Feist         if (!redfish::json_util::readJson(
1060b6baeaa4SJames Feist                 it.value(), response->res, "Zones", zones, "Steps", steps,
1061b6baeaa4SJames Feist                 "Inputs", inputs, "PositiveHysteresis", positiveHysteresis,
1062c33a90ecSJames Feist                 "NegativeHysteresis", negativeHysteresis, "Direction",
1063c33a90ecSJames Feist                 direction))
10645f2caaefSJames Feist         {
10655f2caaefSJames Feist             BMCWEB_LOG_ERROR << "Line:" << __LINE__ << ", Illegal Property "
1066b6baeaa4SJames Feist                              << it.value().dump();
106783ff9ab6SJames Feist             return CreatePIDRet::fail;
106883ff9ab6SJames Feist         }
10695f2caaefSJames Feist 
10705f2caaefSJames Feist         if (zones)
107183ff9ab6SJames Feist         {
1072b6baeaa4SJames Feist             std::vector<std::string> zonesStrs;
1073b6baeaa4SJames Feist             if (!getZonesFromJsonReq(response, *zones, zonesStrs))
10745f2caaefSJames Feist             {
10755f2caaefSJames Feist                 BMCWEB_LOG_ERROR << "Line:" << __LINE__ << ", Illegal Zones";
107683ff9ab6SJames Feist                 return CreatePIDRet::fail;
107783ff9ab6SJames Feist             }
1078b6baeaa4SJames Feist             if (chassis.empty() &&
1079b6baeaa4SJames Feist                 !findChassis(managedObj, zonesStrs[0], chassis))
1080b6baeaa4SJames Feist             {
1081b6baeaa4SJames Feist                 BMCWEB_LOG_ERROR << "Failed to get chassis from config patch";
1082b6baeaa4SJames Feist                 messages::invalidObject(response->res, it.key());
1083b6baeaa4SJames Feist                 return CreatePIDRet::fail;
1084b6baeaa4SJames Feist             }
1085b6baeaa4SJames Feist             output["Zones"] = std::move(zonesStrs);
10865f2caaefSJames Feist         }
10875f2caaefSJames Feist         if (steps)
10885f2caaefSJames Feist         {
10895f2caaefSJames Feist             std::vector<double> readings;
10905f2caaefSJames Feist             std::vector<double> outputs;
10915f2caaefSJames Feist             for (auto& step : *steps)
10925f2caaefSJames Feist             {
10935f2caaefSJames Feist                 double target;
109423a21a1cSEd Tanous                 double out;
10955f2caaefSJames Feist 
10965f2caaefSJames Feist                 if (!redfish::json_util::readJson(step, response->res, "Target",
109723a21a1cSEd Tanous                                                   target, "Output", out))
10985f2caaefSJames Feist                 {
10995f2caaefSJames Feist                     BMCWEB_LOG_ERROR << "Line:" << __LINE__
1100b6baeaa4SJames Feist                                      << ", Illegal Property "
1101b6baeaa4SJames Feist                                      << it.value().dump();
11025f2caaefSJames Feist                     return CreatePIDRet::fail;
11035f2caaefSJames Feist                 }
11045f2caaefSJames Feist                 readings.emplace_back(target);
110523a21a1cSEd Tanous                 outputs.emplace_back(out);
11065f2caaefSJames Feist             }
11075f2caaefSJames Feist             output["Reading"] = std::move(readings);
11085f2caaefSJames Feist             output["Output"] = std::move(outputs);
11095f2caaefSJames Feist         }
11105f2caaefSJames Feist         if (inputs)
11115f2caaefSJames Feist         {
11125f2caaefSJames Feist             for (std::string& value : *inputs)
11135f2caaefSJames Feist             {
11145f2caaefSJames Feist                 boost::replace_all(value, "_", " ");
11155f2caaefSJames Feist             }
11165f2caaefSJames Feist             output["Inputs"] = std::move(*inputs);
11175f2caaefSJames Feist         }
11185f2caaefSJames Feist         if (negativeHysteresis)
11195f2caaefSJames Feist         {
11205f2caaefSJames Feist             output["NegativeHysteresis"] = *negativeHysteresis;
11215f2caaefSJames Feist         }
11225f2caaefSJames Feist         if (positiveHysteresis)
11235f2caaefSJames Feist         {
11245f2caaefSJames Feist             output["PositiveHysteresis"] = *positiveHysteresis;
112583ff9ab6SJames Feist         }
1126c33a90ecSJames Feist         if (direction)
1127c33a90ecSJames Feist         {
1128c33a90ecSJames Feist             constexpr const std::array<const char*, 2> allowedDirections = {
1129c33a90ecSJames Feist                 "Ceiling", "Floor"};
1130c33a90ecSJames Feist             if (std::find(allowedDirections.begin(), allowedDirections.end(),
1131c33a90ecSJames Feist                           *direction) == allowedDirections.end())
1132c33a90ecSJames Feist             {
1133c33a90ecSJames Feist                 messages::propertyValueTypeError(response->res, "Direction",
1134c33a90ecSJames Feist                                                  *direction);
1135c33a90ecSJames Feist                 return CreatePIDRet::fail;
1136c33a90ecSJames Feist             }
1137c33a90ecSJames Feist             output["Class"] = *direction;
1138c33a90ecSJames Feist         }
113983ff9ab6SJames Feist     }
114083ff9ab6SJames Feist     else
114183ff9ab6SJames Feist     {
11425f2caaefSJames Feist         BMCWEB_LOG_ERROR << "Line:" << __LINE__ << ", Illegal Type " << type;
114335a62c7cSJason M. Bills         messages::propertyUnknown(response->res, type);
114483ff9ab6SJames Feist         return CreatePIDRet::fail;
114583ff9ab6SJames Feist     }
114683ff9ab6SJames Feist     return CreatePIDRet::patch;
114783ff9ab6SJames Feist }
114873df0db0SJames Feist struct GetPIDValues : std::enable_shared_from_this<GetPIDValues>
114973df0db0SJames Feist {
115083ff9ab6SJames Feist 
115123a21a1cSEd Tanous     GetPIDValues(const std::shared_ptr<AsyncResp>& asyncRespIn) :
115223a21a1cSEd Tanous         asyncResp(asyncRespIn)
115373df0db0SJames Feist 
11541214b7e7SGunnar Mills     {}
11559c310685SBorawski.Lukasz 
115673df0db0SJames Feist     void run()
11575b4aa86bSJames Feist     {
115873df0db0SJames Feist         std::shared_ptr<GetPIDValues> self = shared_from_this();
115973df0db0SJames Feist 
116073df0db0SJames Feist         // get all configurations
11615b4aa86bSJames Feist         crow::connections::systemBus->async_method_call(
116273df0db0SJames Feist             [self](const boost::system::error_code ec,
116323a21a1cSEd Tanous                    const crow::openbmc_mapper::GetSubTreeType& subtreeLocal) {
11645b4aa86bSJames Feist                 if (ec)
11655b4aa86bSJames Feist                 {
11665b4aa86bSJames Feist                     BMCWEB_LOG_ERROR << ec;
116773df0db0SJames Feist                     messages::internalError(self->asyncResp->res);
116873df0db0SJames Feist                     return;
116973df0db0SJames Feist                 }
117023a21a1cSEd Tanous                 self->subtree = subtreeLocal;
117173df0db0SJames Feist             },
117273df0db0SJames Feist             "xyz.openbmc_project.ObjectMapper",
117373df0db0SJames Feist             "/xyz/openbmc_project/object_mapper",
117473df0db0SJames Feist             "xyz.openbmc_project.ObjectMapper", "GetSubTree", "/", 0,
117573df0db0SJames Feist             std::array<const char*, 4>{
117673df0db0SJames Feist                 pidConfigurationIface, pidZoneConfigurationIface,
117773df0db0SJames Feist                 objectManagerIface, stepwiseConfigurationIface});
117873df0db0SJames Feist 
117973df0db0SJames Feist         // at the same time get the selected profile
118073df0db0SJames Feist         crow::connections::systemBus->async_method_call(
118173df0db0SJames Feist             [self](const boost::system::error_code ec,
118223a21a1cSEd Tanous                    const crow::openbmc_mapper::GetSubTreeType& subtreeLocal) {
118323a21a1cSEd Tanous                 if (ec || subtreeLocal.empty())
118473df0db0SJames Feist                 {
118573df0db0SJames Feist                     return;
118673df0db0SJames Feist                 }
118723a21a1cSEd Tanous                 if (subtreeLocal[0].second.size() != 1)
118873df0db0SJames Feist                 {
118973df0db0SJames Feist                     // invalid mapper response, should never happen
119073df0db0SJames Feist                     BMCWEB_LOG_ERROR << "GetPIDValues: Mapper Error";
119173df0db0SJames Feist                     messages::internalError(self->asyncResp->res);
11925b4aa86bSJames Feist                     return;
11935b4aa86bSJames Feist                 }
11945b4aa86bSJames Feist 
119523a21a1cSEd Tanous                 const std::string& path = subtreeLocal[0].first;
119623a21a1cSEd Tanous                 const std::string& owner = subtreeLocal[0].second[0].first;
119773df0db0SJames Feist                 crow::connections::systemBus->async_method_call(
119873df0db0SJames Feist                     [path, owner, self](
119923a21a1cSEd Tanous                         const boost::system::error_code ec2,
120073df0db0SJames Feist                         const boost::container::flat_map<
120173df0db0SJames Feist                             std::string, std::variant<std::vector<std::string>,
120273df0db0SJames Feist                                                       std::string>>& resp) {
120323a21a1cSEd Tanous                         if (ec2)
120473df0db0SJames Feist                         {
120573df0db0SJames Feist                             BMCWEB_LOG_ERROR << "GetPIDValues: Can't get "
120673df0db0SJames Feist                                                 "thermalModeIface "
120773df0db0SJames Feist                                              << path;
120873df0db0SJames Feist                             messages::internalError(self->asyncResp->res);
120973df0db0SJames Feist                             return;
121073df0db0SJames Feist                         }
1211271584abSEd Tanous                         const std::string* current = nullptr;
1212271584abSEd Tanous                         const std::vector<std::string>* supported = nullptr;
121373df0db0SJames Feist                         for (auto& [key, value] : resp)
121473df0db0SJames Feist                         {
121573df0db0SJames Feist                             if (key == "Current")
121673df0db0SJames Feist                             {
121773df0db0SJames Feist                                 current = std::get_if<std::string>(&value);
121873df0db0SJames Feist                                 if (current == nullptr)
121973df0db0SJames Feist                                 {
122073df0db0SJames Feist                                     BMCWEB_LOG_ERROR
122173df0db0SJames Feist                                         << "GetPIDValues: thermal mode "
122273df0db0SJames Feist                                            "iface invalid "
122373df0db0SJames Feist                                         << path;
122473df0db0SJames Feist                                     messages::internalError(
122573df0db0SJames Feist                                         self->asyncResp->res);
122673df0db0SJames Feist                                     return;
122773df0db0SJames Feist                                 }
122873df0db0SJames Feist                             }
122973df0db0SJames Feist                             if (key == "Supported")
123073df0db0SJames Feist                             {
123173df0db0SJames Feist                                 supported =
123273df0db0SJames Feist                                     std::get_if<std::vector<std::string>>(
123373df0db0SJames Feist                                         &value);
123473df0db0SJames Feist                                 if (supported == nullptr)
123573df0db0SJames Feist                                 {
123673df0db0SJames Feist                                     BMCWEB_LOG_ERROR
123773df0db0SJames Feist                                         << "GetPIDValues: thermal mode "
123873df0db0SJames Feist                                            "iface invalid"
123973df0db0SJames Feist                                         << path;
124073df0db0SJames Feist                                     messages::internalError(
124173df0db0SJames Feist                                         self->asyncResp->res);
124273df0db0SJames Feist                                     return;
124373df0db0SJames Feist                                 }
124473df0db0SJames Feist                             }
124573df0db0SJames Feist                         }
124673df0db0SJames Feist                         if (current == nullptr || supported == nullptr)
124773df0db0SJames Feist                         {
124873df0db0SJames Feist                             BMCWEB_LOG_ERROR << "GetPIDValues: thermal mode "
124973df0db0SJames Feist                                                 "iface invalid "
125073df0db0SJames Feist                                              << path;
125173df0db0SJames Feist                             messages::internalError(self->asyncResp->res);
125273df0db0SJames Feist                             return;
125373df0db0SJames Feist                         }
125473df0db0SJames Feist                         self->currentProfile = *current;
125573df0db0SJames Feist                         self->supportedProfiles = *supported;
125673df0db0SJames Feist                     },
125773df0db0SJames Feist                     owner, path, "org.freedesktop.DBus.Properties", "GetAll",
125873df0db0SJames Feist                     thermalModeIface);
125973df0db0SJames Feist             },
126073df0db0SJames Feist             "xyz.openbmc_project.ObjectMapper",
126173df0db0SJames Feist             "/xyz/openbmc_project/object_mapper",
126273df0db0SJames Feist             "xyz.openbmc_project.ObjectMapper", "GetSubTree", "/", 0,
126373df0db0SJames Feist             std::array<const char*, 1>{thermalModeIface});
126473df0db0SJames Feist     }
126573df0db0SJames Feist 
126673df0db0SJames Feist     ~GetPIDValues()
126773df0db0SJames Feist     {
126873df0db0SJames Feist         if (asyncResp->res.result() != boost::beast::http::status::ok)
126973df0db0SJames Feist         {
127073df0db0SJames Feist             return;
127173df0db0SJames Feist         }
12725b4aa86bSJames Feist         // create map of <connection, path to objMgr>>
127373df0db0SJames Feist         boost::container::flat_map<std::string, std::string> objectMgrPaths;
12746bce33bcSJames Feist         boost::container::flat_set<std::string> calledConnections;
12755b4aa86bSJames Feist         for (const auto& pathGroup : subtree)
12765b4aa86bSJames Feist         {
12775b4aa86bSJames Feist             for (const auto& connectionGroup : pathGroup.second)
12785b4aa86bSJames Feist             {
12796bce33bcSJames Feist                 auto findConnection =
12806bce33bcSJames Feist                     calledConnections.find(connectionGroup.first);
12816bce33bcSJames Feist                 if (findConnection != calledConnections.end())
12826bce33bcSJames Feist                 {
12836bce33bcSJames Feist                     break;
12846bce33bcSJames Feist                 }
128573df0db0SJames Feist                 for (const std::string& interface : connectionGroup.second)
12865b4aa86bSJames Feist                 {
12875b4aa86bSJames Feist                     if (interface == objectManagerIface)
12885b4aa86bSJames Feist                     {
128973df0db0SJames Feist                         objectMgrPaths[connectionGroup.first] = pathGroup.first;
12905b4aa86bSJames Feist                     }
12915b4aa86bSJames Feist                     // this list is alphabetical, so we
12925b4aa86bSJames Feist                     // should have found the objMgr by now
12935b4aa86bSJames Feist                     if (interface == pidConfigurationIface ||
1294b7a08d04SJames Feist                         interface == pidZoneConfigurationIface ||
1295b7a08d04SJames Feist                         interface == stepwiseConfigurationIface)
12965b4aa86bSJames Feist                     {
12975b4aa86bSJames Feist                         auto findObjMgr =
12985b4aa86bSJames Feist                             objectMgrPaths.find(connectionGroup.first);
12995b4aa86bSJames Feist                         if (findObjMgr == objectMgrPaths.end())
13005b4aa86bSJames Feist                         {
13015b4aa86bSJames Feist                             BMCWEB_LOG_DEBUG << connectionGroup.first
13025b4aa86bSJames Feist                                              << "Has no Object Manager";
13035b4aa86bSJames Feist                             continue;
13045b4aa86bSJames Feist                         }
13056bce33bcSJames Feist 
13066bce33bcSJames Feist                         calledConnections.insert(connectionGroup.first);
13076bce33bcSJames Feist 
130873df0db0SJames Feist                         asyncPopulatePid(findObjMgr->first, findObjMgr->second,
130973df0db0SJames Feist                                          currentProfile, supportedProfiles,
131073df0db0SJames Feist                                          asyncResp);
13115b4aa86bSJames Feist                         break;
13125b4aa86bSJames Feist                     }
13135b4aa86bSJames Feist                 }
13145b4aa86bSJames Feist             }
13155b4aa86bSJames Feist         }
131673df0db0SJames Feist     }
131773df0db0SJames Feist 
131873df0db0SJames Feist     std::vector<std::string> supportedProfiles;
131973df0db0SJames Feist     std::string currentProfile;
132073df0db0SJames Feist     crow::openbmc_mapper::GetSubTreeType subtree;
132173df0db0SJames Feist     std::shared_ptr<AsyncResp> asyncResp;
132273df0db0SJames Feist };
132373df0db0SJames Feist 
132473df0db0SJames Feist struct SetPIDValues : std::enable_shared_from_this<SetPIDValues>
132573df0db0SJames Feist {
132673df0db0SJames Feist 
1327271584abSEd Tanous     SetPIDValues(const std::shared_ptr<AsyncResp>& asyncRespIn,
132873df0db0SJames Feist                  nlohmann::json& data) :
1329271584abSEd Tanous         asyncResp(asyncRespIn)
133073df0db0SJames Feist     {
133173df0db0SJames Feist 
133273df0db0SJames Feist         std::optional<nlohmann::json> pidControllers;
133373df0db0SJames Feist         std::optional<nlohmann::json> fanControllers;
133473df0db0SJames Feist         std::optional<nlohmann::json> fanZones;
133573df0db0SJames Feist         std::optional<nlohmann::json> stepwiseControllers;
133673df0db0SJames Feist 
133773df0db0SJames Feist         if (!redfish::json_util::readJson(
133873df0db0SJames Feist                 data, asyncResp->res, "PidControllers", pidControllers,
133973df0db0SJames Feist                 "FanControllers", fanControllers, "FanZones", fanZones,
134073df0db0SJames Feist                 "StepwiseControllers", stepwiseControllers, "Profile", profile))
134173df0db0SJames Feist         {
134273df0db0SJames Feist             BMCWEB_LOG_ERROR << "Line:" << __LINE__ << ", Illegal Property "
134373df0db0SJames Feist                              << data.dump();
134473df0db0SJames Feist             return;
134573df0db0SJames Feist         }
134673df0db0SJames Feist         configuration.emplace_back("PidControllers", std::move(pidControllers));
134773df0db0SJames Feist         configuration.emplace_back("FanControllers", std::move(fanControllers));
134873df0db0SJames Feist         configuration.emplace_back("FanZones", std::move(fanZones));
134973df0db0SJames Feist         configuration.emplace_back("StepwiseControllers",
135073df0db0SJames Feist                                    std::move(stepwiseControllers));
135173df0db0SJames Feist     }
135273df0db0SJames Feist     void run()
135373df0db0SJames Feist     {
135473df0db0SJames Feist         if (asyncResp->res.result() != boost::beast::http::status::ok)
135573df0db0SJames Feist         {
135673df0db0SJames Feist             return;
135773df0db0SJames Feist         }
135873df0db0SJames Feist 
135973df0db0SJames Feist         std::shared_ptr<SetPIDValues> self = shared_from_this();
136073df0db0SJames Feist 
136173df0db0SJames Feist         // todo(james): might make sense to do a mapper call here if this
136273df0db0SJames Feist         // interface gets more traction
136373df0db0SJames Feist         crow::connections::systemBus->async_method_call(
136473df0db0SJames Feist             [self](const boost::system::error_code ec,
1365271584abSEd Tanous                    dbus::utility::ManagedObjectType& mObj) {
136673df0db0SJames Feist                 if (ec)
136773df0db0SJames Feist                 {
136873df0db0SJames Feist                     BMCWEB_LOG_ERROR << "Error communicating to Entity Manager";
136973df0db0SJames Feist                     messages::internalError(self->asyncResp->res);
137073df0db0SJames Feist                     return;
137173df0db0SJames Feist                 }
1372e69d9de2SJames Feist                 const std::array<const char*, 3> configurations = {
1373e69d9de2SJames Feist                     pidConfigurationIface, pidZoneConfigurationIface,
1374e69d9de2SJames Feist                     stepwiseConfigurationIface};
1375e69d9de2SJames Feist 
137614b0b8d5SJames Feist                 for (const auto& [path, object] : mObj)
1377e69d9de2SJames Feist                 {
137814b0b8d5SJames Feist                     for (const auto& [interface, _] : object)
1379e69d9de2SJames Feist                     {
1380e69d9de2SJames Feist                         if (std::find(configurations.begin(),
1381e69d9de2SJames Feist                                       configurations.end(),
1382e69d9de2SJames Feist                                       interface) != configurations.end())
1383e69d9de2SJames Feist                         {
138414b0b8d5SJames Feist                             self->objectCount++;
1385e69d9de2SJames Feist                             break;
1386e69d9de2SJames Feist                         }
1387e69d9de2SJames Feist                     }
1388e69d9de2SJames Feist                 }
1389271584abSEd Tanous                 self->managedObj = std::move(mObj);
139073df0db0SJames Feist             },
139173df0db0SJames Feist             "xyz.openbmc_project.EntityManager", "/", objectManagerIface,
139273df0db0SJames Feist             "GetManagedObjects");
139373df0db0SJames Feist 
139473df0db0SJames Feist         // at the same time get the profile information
139573df0db0SJames Feist         crow::connections::systemBus->async_method_call(
139673df0db0SJames Feist             [self](const boost::system::error_code ec,
139773df0db0SJames Feist                    const crow::openbmc_mapper::GetSubTreeType& subtree) {
139873df0db0SJames Feist                 if (ec || subtree.empty())
139973df0db0SJames Feist                 {
140073df0db0SJames Feist                     return;
140173df0db0SJames Feist                 }
140273df0db0SJames Feist                 if (subtree[0].second.empty())
140373df0db0SJames Feist                 {
140473df0db0SJames Feist                     // invalid mapper response, should never happen
140573df0db0SJames Feist                     BMCWEB_LOG_ERROR << "SetPIDValues: Mapper Error";
140673df0db0SJames Feist                     messages::internalError(self->asyncResp->res);
140773df0db0SJames Feist                     return;
140873df0db0SJames Feist                 }
140973df0db0SJames Feist 
141073df0db0SJames Feist                 const std::string& path = subtree[0].first;
141173df0db0SJames Feist                 const std::string& owner = subtree[0].second[0].first;
141273df0db0SJames Feist                 crow::connections::systemBus->async_method_call(
141373df0db0SJames Feist                     [self, path, owner](
1414cb13a392SEd Tanous                         const boost::system::error_code ec2,
141573df0db0SJames Feist                         const boost::container::flat_map<
141673df0db0SJames Feist                             std::string, std::variant<std::vector<std::string>,
1417271584abSEd Tanous                                                       std::string>>& r) {
1418cb13a392SEd Tanous                         if (ec2)
141973df0db0SJames Feist                         {
142073df0db0SJames Feist                             BMCWEB_LOG_ERROR << "SetPIDValues: Can't get "
142173df0db0SJames Feist                                                 "thermalModeIface "
142273df0db0SJames Feist                                              << path;
142373df0db0SJames Feist                             messages::internalError(self->asyncResp->res);
142473df0db0SJames Feist                             return;
142573df0db0SJames Feist                         }
1426271584abSEd Tanous                         const std::string* current = nullptr;
1427271584abSEd Tanous                         const std::vector<std::string>* supported = nullptr;
1428271584abSEd Tanous                         for (auto& [key, value] : r)
142973df0db0SJames Feist                         {
143073df0db0SJames Feist                             if (key == "Current")
143173df0db0SJames Feist                             {
143273df0db0SJames Feist                                 current = std::get_if<std::string>(&value);
143373df0db0SJames Feist                                 if (current == nullptr)
143473df0db0SJames Feist                                 {
143573df0db0SJames Feist                                     BMCWEB_LOG_ERROR
143673df0db0SJames Feist                                         << "SetPIDValues: thermal mode "
143773df0db0SJames Feist                                            "iface invalid "
143873df0db0SJames Feist                                         << path;
143973df0db0SJames Feist                                     messages::internalError(
144073df0db0SJames Feist                                         self->asyncResp->res);
144173df0db0SJames Feist                                     return;
144273df0db0SJames Feist                                 }
144373df0db0SJames Feist                             }
144473df0db0SJames Feist                             if (key == "Supported")
144573df0db0SJames Feist                             {
144673df0db0SJames Feist                                 supported =
144773df0db0SJames Feist                                     std::get_if<std::vector<std::string>>(
144873df0db0SJames Feist                                         &value);
144973df0db0SJames Feist                                 if (supported == nullptr)
145073df0db0SJames Feist                                 {
145173df0db0SJames Feist                                     BMCWEB_LOG_ERROR
145273df0db0SJames Feist                                         << "SetPIDValues: thermal mode "
145373df0db0SJames Feist                                            "iface invalid"
145473df0db0SJames Feist                                         << path;
145573df0db0SJames Feist                                     messages::internalError(
145673df0db0SJames Feist                                         self->asyncResp->res);
145773df0db0SJames Feist                                     return;
145873df0db0SJames Feist                                 }
145973df0db0SJames Feist                             }
146073df0db0SJames Feist                         }
146173df0db0SJames Feist                         if (current == nullptr || supported == nullptr)
146273df0db0SJames Feist                         {
146373df0db0SJames Feist                             BMCWEB_LOG_ERROR << "SetPIDValues: thermal mode "
146473df0db0SJames Feist                                                 "iface invalid "
146573df0db0SJames Feist                                              << path;
146673df0db0SJames Feist                             messages::internalError(self->asyncResp->res);
146773df0db0SJames Feist                             return;
146873df0db0SJames Feist                         }
146973df0db0SJames Feist                         self->currentProfile = *current;
147073df0db0SJames Feist                         self->supportedProfiles = *supported;
147173df0db0SJames Feist                         self->profileConnection = owner;
147273df0db0SJames Feist                         self->profilePath = path;
147373df0db0SJames Feist                     },
147473df0db0SJames Feist                     owner, path, "org.freedesktop.DBus.Properties", "GetAll",
147573df0db0SJames Feist                     thermalModeIface);
14765b4aa86bSJames Feist             },
14775b4aa86bSJames Feist             "xyz.openbmc_project.ObjectMapper",
14785b4aa86bSJames Feist             "/xyz/openbmc_project/object_mapper",
14795b4aa86bSJames Feist             "xyz.openbmc_project.ObjectMapper", "GetSubTree", "/", 0,
148073df0db0SJames Feist             std::array<const char*, 1>{thermalModeIface});
148173df0db0SJames Feist     }
148273df0db0SJames Feist     ~SetPIDValues()
148373df0db0SJames Feist     {
148473df0db0SJames Feist         if (asyncResp->res.result() != boost::beast::http::status::ok)
148573df0db0SJames Feist         {
148673df0db0SJames Feist             return;
14875b4aa86bSJames Feist         }
14885b4aa86bSJames Feist 
148973df0db0SJames Feist         std::shared_ptr<AsyncResp> response = asyncResp;
149073df0db0SJames Feist 
149173df0db0SJames Feist         if (profile)
149273df0db0SJames Feist         {
149373df0db0SJames Feist             if (std::find(supportedProfiles.begin(), supportedProfiles.end(),
149473df0db0SJames Feist                           *profile) == supportedProfiles.end())
149573df0db0SJames Feist             {
149673df0db0SJames Feist                 messages::actionParameterUnknown(response->res, "Profile",
149773df0db0SJames Feist                                                  *profile);
149873df0db0SJames Feist                 return;
149973df0db0SJames Feist             }
150073df0db0SJames Feist             currentProfile = *profile;
150173df0db0SJames Feist             crow::connections::systemBus->async_method_call(
150273df0db0SJames Feist                 [response](const boost::system::error_code ec) {
150373df0db0SJames Feist                     if (ec)
150473df0db0SJames Feist                     {
150573df0db0SJames Feist                         BMCWEB_LOG_ERROR << "Error patching profile" << ec;
150673df0db0SJames Feist                         messages::internalError(response->res);
150773df0db0SJames Feist                     }
150873df0db0SJames Feist                 },
150973df0db0SJames Feist                 profileConnection, profilePath,
151073df0db0SJames Feist                 "org.freedesktop.DBus.Properties", "Set", thermalModeIface,
151173df0db0SJames Feist                 "Current", std::variant<std::string>(*profile));
151273df0db0SJames Feist         }
151373df0db0SJames Feist 
151473df0db0SJames Feist         for (auto& containerPair : configuration)
151573df0db0SJames Feist         {
151673df0db0SJames Feist             auto& container = containerPair.second;
151773df0db0SJames Feist             if (!container)
151873df0db0SJames Feist             {
151973df0db0SJames Feist                 continue;
152073df0db0SJames Feist             }
15216ee7f774SJames Feist             BMCWEB_LOG_DEBUG << *container;
15226ee7f774SJames Feist 
152373df0db0SJames Feist             std::string& type = containerPair.first;
152473df0db0SJames Feist 
152573df0db0SJames Feist             for (nlohmann::json::iterator it = container->begin();
152673df0db0SJames Feist                  it != container->end(); it++)
152773df0db0SJames Feist             {
152873df0db0SJames Feist                 const auto& name = it.key();
15296ee7f774SJames Feist                 BMCWEB_LOG_DEBUG << "looking for " << name;
15306ee7f774SJames Feist 
153173df0db0SJames Feist                 auto pathItr =
153273df0db0SJames Feist                     std::find_if(managedObj.begin(), managedObj.end(),
153373df0db0SJames Feist                                  [&name](const auto& obj) {
153473df0db0SJames Feist                                      return boost::algorithm::ends_with(
153573df0db0SJames Feist                                          obj.first.str, "/" + name);
153673df0db0SJames Feist                                  });
153773df0db0SJames Feist                 boost::container::flat_map<std::string,
153873df0db0SJames Feist                                            dbus::utility::DbusVariantType>
153973df0db0SJames Feist                     output;
154073df0db0SJames Feist 
154173df0db0SJames Feist                 output.reserve(16); // The pid interface length
154273df0db0SJames Feist 
154373df0db0SJames Feist                 // determines if we're patching entity-manager or
154473df0db0SJames Feist                 // creating a new object
154573df0db0SJames Feist                 bool createNewObject = (pathItr == managedObj.end());
15466ee7f774SJames Feist                 BMCWEB_LOG_DEBUG << "Found = " << !createNewObject;
15476ee7f774SJames Feist 
154873df0db0SJames Feist                 std::string iface;
154973df0db0SJames Feist                 if (type == "PidControllers" || type == "FanControllers")
155073df0db0SJames Feist                 {
155173df0db0SJames Feist                     iface = pidConfigurationIface;
155273df0db0SJames Feist                     if (!createNewObject &&
155373df0db0SJames Feist                         pathItr->second.find(pidConfigurationIface) ==
155473df0db0SJames Feist                             pathItr->second.end())
155573df0db0SJames Feist                     {
155673df0db0SJames Feist                         createNewObject = true;
155773df0db0SJames Feist                     }
155873df0db0SJames Feist                 }
155973df0db0SJames Feist                 else if (type == "FanZones")
156073df0db0SJames Feist                 {
156173df0db0SJames Feist                     iface = pidZoneConfigurationIface;
156273df0db0SJames Feist                     if (!createNewObject &&
156373df0db0SJames Feist                         pathItr->second.find(pidZoneConfigurationIface) ==
156473df0db0SJames Feist                             pathItr->second.end())
156573df0db0SJames Feist                     {
156673df0db0SJames Feist 
156773df0db0SJames Feist                         createNewObject = true;
156873df0db0SJames Feist                     }
156973df0db0SJames Feist                 }
157073df0db0SJames Feist                 else if (type == "StepwiseControllers")
157173df0db0SJames Feist                 {
157273df0db0SJames Feist                     iface = stepwiseConfigurationIface;
157373df0db0SJames Feist                     if (!createNewObject &&
157473df0db0SJames Feist                         pathItr->second.find(stepwiseConfigurationIface) ==
157573df0db0SJames Feist                             pathItr->second.end())
157673df0db0SJames Feist                     {
157773df0db0SJames Feist                         createNewObject = true;
157873df0db0SJames Feist                     }
157973df0db0SJames Feist                 }
15806ee7f774SJames Feist 
15816ee7f774SJames Feist                 if (createNewObject && it.value() == nullptr)
15826ee7f774SJames Feist                 {
15834e0453b1SGunnar Mills                     // can't delete a non-existent object
15846ee7f774SJames Feist                     messages::invalidObject(response->res, name);
15856ee7f774SJames Feist                     continue;
15866ee7f774SJames Feist                 }
15876ee7f774SJames Feist 
15886ee7f774SJames Feist                 std::string path;
15896ee7f774SJames Feist                 if (pathItr != managedObj.end())
15906ee7f774SJames Feist                 {
15916ee7f774SJames Feist                     path = pathItr->first.str;
15926ee7f774SJames Feist                 }
15936ee7f774SJames Feist 
159473df0db0SJames Feist                 BMCWEB_LOG_DEBUG << "Create new = " << createNewObject << "\n";
1595e69d9de2SJames Feist 
1596e69d9de2SJames Feist                 // arbitrary limit to avoid attacks
1597e69d9de2SJames Feist                 constexpr const size_t controllerLimit = 500;
159814b0b8d5SJames Feist                 if (createNewObject && objectCount >= controllerLimit)
1599e69d9de2SJames Feist                 {
1600e69d9de2SJames Feist                     messages::resourceExhaustion(response->res, type);
1601e69d9de2SJames Feist                     continue;
1602e69d9de2SJames Feist                 }
1603e69d9de2SJames Feist 
160473df0db0SJames Feist                 output["Name"] = boost::replace_all_copy(name, "_", " ");
160573df0db0SJames Feist 
160673df0db0SJames Feist                 std::string chassis;
160773df0db0SJames Feist                 CreatePIDRet ret = createPidInterface(
16086ee7f774SJames Feist                     response, type, it, path, managedObj, createNewObject,
16096ee7f774SJames Feist                     output, chassis, currentProfile);
161073df0db0SJames Feist                 if (ret == CreatePIDRet::fail)
161173df0db0SJames Feist                 {
161273df0db0SJames Feist                     return;
161373df0db0SJames Feist                 }
161473df0db0SJames Feist                 else if (ret == CreatePIDRet::del)
161573df0db0SJames Feist                 {
161673df0db0SJames Feist                     continue;
161773df0db0SJames Feist                 }
161873df0db0SJames Feist 
161973df0db0SJames Feist                 if (!createNewObject)
162073df0db0SJames Feist                 {
162173df0db0SJames Feist                     for (const auto& property : output)
162273df0db0SJames Feist                     {
162373df0db0SJames Feist                         crow::connections::systemBus->async_method_call(
162473df0db0SJames Feist                             [response,
162573df0db0SJames Feist                              propertyName{std::string(property.first)}](
162673df0db0SJames Feist                                 const boost::system::error_code ec) {
162773df0db0SJames Feist                                 if (ec)
162873df0db0SJames Feist                                 {
162973df0db0SJames Feist                                     BMCWEB_LOG_ERROR << "Error patching "
163073df0db0SJames Feist                                                      << propertyName << ": "
163173df0db0SJames Feist                                                      << ec;
163273df0db0SJames Feist                                     messages::internalError(response->res);
163373df0db0SJames Feist                                     return;
163473df0db0SJames Feist                                 }
163573df0db0SJames Feist                                 messages::success(response->res);
163673df0db0SJames Feist                             },
16376ee7f774SJames Feist                             "xyz.openbmc_project.EntityManager", path,
163873df0db0SJames Feist                             "org.freedesktop.DBus.Properties", "Set", iface,
163973df0db0SJames Feist                             property.first, property.second);
164073df0db0SJames Feist                     }
164173df0db0SJames Feist                 }
164273df0db0SJames Feist                 else
164373df0db0SJames Feist                 {
164473df0db0SJames Feist                     if (chassis.empty())
164573df0db0SJames Feist                     {
164673df0db0SJames Feist                         BMCWEB_LOG_ERROR << "Failed to get chassis from config";
164773df0db0SJames Feist                         messages::invalidObject(response->res, name);
164873df0db0SJames Feist                         return;
164973df0db0SJames Feist                     }
165073df0db0SJames Feist 
165173df0db0SJames Feist                     bool foundChassis = false;
165273df0db0SJames Feist                     for (const auto& obj : managedObj)
165373df0db0SJames Feist                     {
165473df0db0SJames Feist                         if (boost::algorithm::ends_with(obj.first.str, chassis))
165573df0db0SJames Feist                         {
165673df0db0SJames Feist                             chassis = obj.first.str;
165773df0db0SJames Feist                             foundChassis = true;
165873df0db0SJames Feist                             break;
165973df0db0SJames Feist                         }
166073df0db0SJames Feist                     }
166173df0db0SJames Feist                     if (!foundChassis)
166273df0db0SJames Feist                     {
166373df0db0SJames Feist                         BMCWEB_LOG_ERROR << "Failed to find chassis on dbus";
166473df0db0SJames Feist                         messages::resourceMissingAtURI(
166573df0db0SJames Feist                             response->res, "/redfish/v1/Chassis/" + chassis);
166673df0db0SJames Feist                         return;
166773df0db0SJames Feist                     }
166873df0db0SJames Feist 
166973df0db0SJames Feist                     crow::connections::systemBus->async_method_call(
167073df0db0SJames Feist                         [response](const boost::system::error_code ec) {
167173df0db0SJames Feist                             if (ec)
167273df0db0SJames Feist                             {
167373df0db0SJames Feist                                 BMCWEB_LOG_ERROR << "Error Adding Pid Object "
167473df0db0SJames Feist                                                  << ec;
167573df0db0SJames Feist                                 messages::internalError(response->res);
167673df0db0SJames Feist                                 return;
167773df0db0SJames Feist                             }
167873df0db0SJames Feist                             messages::success(response->res);
167973df0db0SJames Feist                         },
168073df0db0SJames Feist                         "xyz.openbmc_project.EntityManager", chassis,
168173df0db0SJames Feist                         "xyz.openbmc_project.AddObject", "AddObject", output);
168273df0db0SJames Feist                 }
168373df0db0SJames Feist             }
168473df0db0SJames Feist         }
168573df0db0SJames Feist     }
168673df0db0SJames Feist     std::shared_ptr<AsyncResp> asyncResp;
168773df0db0SJames Feist     std::vector<std::pair<std::string, std::optional<nlohmann::json>>>
168873df0db0SJames Feist         configuration;
168973df0db0SJames Feist     std::optional<std::string> profile;
169073df0db0SJames Feist     dbus::utility::ManagedObjectType managedObj;
169173df0db0SJames Feist     std::vector<std::string> supportedProfiles;
169273df0db0SJames Feist     std::string currentProfile;
169373df0db0SJames Feist     std::string profileConnection;
169473df0db0SJames Feist     std::string profilePath;
169514b0b8d5SJames Feist     size_t objectCount = 0;
169673df0db0SJames Feist };
169773df0db0SJames Feist 
169873df0db0SJames Feist class Manager : public Node
169973df0db0SJames Feist {
170073df0db0SJames Feist   public:
170152cc112dSEd Tanous     Manager(App& app) : Node(app, "/redfish/v1/Managers/bmc/")
170273df0db0SJames Feist     {
170352cc112dSEd Tanous 
170452cc112dSEd Tanous         uuid = persistent_data::getConfig().systemUuid;
170573df0db0SJames Feist         entityPrivileges = {
170673df0db0SJames Feist             {boost::beast::http::verb::get, {{"Login"}}},
170773df0db0SJames Feist             {boost::beast::http::verb::head, {{"Login"}}},
170873df0db0SJames Feist             {boost::beast::http::verb::patch, {{"ConfigureManager"}}},
170973df0db0SJames Feist             {boost::beast::http::verb::put, {{"ConfigureManager"}}},
171073df0db0SJames Feist             {boost::beast::http::verb::delete_, {{"ConfigureManager"}}},
171173df0db0SJames Feist             {boost::beast::http::verb::post, {{"ConfigureManager"}}}};
171273df0db0SJames Feist     }
171373df0db0SJames Feist 
171473df0db0SJames Feist   private:
1715cb13a392SEd Tanous     void doGet(crow::Response& res, const crow::Request&,
1716cb13a392SEd Tanous                const std::vector<std::string>&) override
17171abe55efSEd Tanous     {
17180f74e643SEd Tanous         res.jsonValue["@odata.id"] = "/redfish/v1/Managers/bmc";
17194bf2b033SGunnar Mills         res.jsonValue["@odata.type"] = "#Manager.v1_9_0.Manager";
17200f74e643SEd Tanous         res.jsonValue["Id"] = "bmc";
17210f74e643SEd Tanous         res.jsonValue["Name"] = "OpenBmc Manager";
17220f74e643SEd Tanous         res.jsonValue["Description"] = "Baseboard Management Controller";
17230f74e643SEd Tanous         res.jsonValue["PowerState"] = "On";
1724029573d4SEd Tanous         res.jsonValue["Status"] = {{"State", "Enabled"}, {"Health", "OK"}};
17250f74e643SEd Tanous         res.jsonValue["ManagerType"] = "BMC";
17263602e232SEd Tanous         res.jsonValue["UUID"] = systemd_utils::getUuid();
17273602e232SEd Tanous         res.jsonValue["ServiceEntryPointUUID"] = uuid;
17280f74e643SEd Tanous         res.jsonValue["Model"] = "OpenBmc"; // TODO(ed), get model
17290f74e643SEd Tanous 
17300f74e643SEd Tanous         res.jsonValue["LogServices"] = {
17310f74e643SEd Tanous             {"@odata.id", "/redfish/v1/Managers/bmc/LogServices"}};
17320f74e643SEd Tanous 
17330f74e643SEd Tanous         res.jsonValue["NetworkProtocol"] = {
17340f74e643SEd Tanous             {"@odata.id", "/redfish/v1/Managers/bmc/NetworkProtocol"}};
17350f74e643SEd Tanous 
17360f74e643SEd Tanous         res.jsonValue["EthernetInterfaces"] = {
17370f74e643SEd Tanous             {"@odata.id", "/redfish/v1/Managers/bmc/EthernetInterfaces"}};
1738107077deSPrzemyslaw Czarnowski 
1739107077deSPrzemyslaw Czarnowski #ifdef BMCWEB_ENABLE_VM_NBDPROXY
1740107077deSPrzemyslaw Czarnowski         res.jsonValue["VirtualMedia"] = {
1741107077deSPrzemyslaw Czarnowski             {"@odata.id", "/redfish/v1/Managers/bmc/VirtualMedia"}};
1742107077deSPrzemyslaw Czarnowski #endif // BMCWEB_ENABLE_VM_NBDPROXY
1743107077deSPrzemyslaw Czarnowski 
17440f74e643SEd Tanous         // default oem data
17450f74e643SEd Tanous         nlohmann::json& oem = res.jsonValue["Oem"];
17460f74e643SEd Tanous         nlohmann::json& oemOpenbmc = oem["OpenBmc"];
17470f74e643SEd Tanous         oem["@odata.type"] = "#OemManager.Oem";
17480f74e643SEd Tanous         oem["@odata.id"] = "/redfish/v1/Managers/bmc#/Oem";
17490f74e643SEd Tanous         oemOpenbmc["@odata.type"] = "#OemManager.OpenBmc";
17500f74e643SEd Tanous         oemOpenbmc["@odata.id"] = "/redfish/v1/Managers/bmc#/Oem/OpenBmc";
1751cfcd5f6bSMarri Devender Rao         oemOpenbmc["Certificates"] = {
1752cfcd5f6bSMarri Devender Rao             {"@odata.id", "/redfish/v1/Managers/bmc/Truststore/Certificates"}};
17530f74e643SEd Tanous 
17542a5c4407SGunnar Mills         // Manager.Reset (an action) can be many values, OpenBMC only supports
17552a5c4407SGunnar Mills         // BMC reboot.
17562a5c4407SGunnar Mills         nlohmann::json& managerReset =
17570f74e643SEd Tanous             res.jsonValue["Actions"]["#Manager.Reset"];
17582a5c4407SGunnar Mills         managerReset["target"] =
1759ed5befbdSJennifer Lee             "/redfish/v1/Managers/bmc/Actions/Manager.Reset";
17601cb1a9e6SAppaRao Puli         managerReset["@Redfish.ActionInfo"] =
17611cb1a9e6SAppaRao Puli             "/redfish/v1/Managers/bmc/ResetActionInfo";
1762ca537928SJennifer Lee 
17633e40fc74SGunnar Mills         // ResetToDefaults (Factory Reset) has values like
17643e40fc74SGunnar Mills         // PreserveNetworkAndUsers and PreserveNetwork that aren't supported
17653e40fc74SGunnar Mills         // on OpenBMC
17663e40fc74SGunnar Mills         nlohmann::json& resetToDefaults =
17673e40fc74SGunnar Mills             res.jsonValue["Actions"]["#Manager.ResetToDefaults"];
17683e40fc74SGunnar Mills         resetToDefaults["target"] =
17693e40fc74SGunnar Mills             "/redfish/v1/Managers/bmc/Actions/Manager.ResetToDefaults";
17703e40fc74SGunnar Mills         resetToDefaults["ResetType@Redfish.AllowableValues"] = {"ResetAll"};
17713e40fc74SGunnar Mills 
1772cb92c03bSAndrew Geissler         res.jsonValue["DateTime"] = crow::utility::dateTimeNow();
1773474bfad5SSantosh Puranik 
1774f8c3e6f0SKuiying Wang         // Fill in SerialConsole info
1775474bfad5SSantosh Puranik         res.jsonValue["SerialConsole"]["ServiceEnabled"] = true;
1776f8c3e6f0SKuiying Wang         res.jsonValue["SerialConsole"]["MaxConcurrentSessions"] = 15;
1777474bfad5SSantosh Puranik         res.jsonValue["SerialConsole"]["ConnectTypesSupported"] = {"IPMI",
1778474bfad5SSantosh Puranik                                                                    "SSH"};
1779ef47bb18SSantosh Puranik #ifdef BMCWEB_ENABLE_KVM
1780f8c3e6f0SKuiying Wang         // Fill in GraphicalConsole info
1781ef47bb18SSantosh Puranik         res.jsonValue["GraphicalConsole"]["ServiceEnabled"] = true;
1782704fae6cSJae Hyun Yoo         res.jsonValue["GraphicalConsole"]["MaxConcurrentSessions"] = 4;
1783ef47bb18SSantosh Puranik         res.jsonValue["GraphicalConsole"]["ConnectTypesSupported"] = {"KVMIP"};
1784ef47bb18SSantosh Puranik #endif // BMCWEB_ENABLE_KVM
1785474bfad5SSantosh Puranik 
1786603a6640SGunnar Mills         res.jsonValue["Links"]["ManagerForServers@odata.count"] = 1;
1787603a6640SGunnar Mills         res.jsonValue["Links"]["ManagerForServers"] = {
1788603a6640SGunnar Mills             {{"@odata.id", "/redfish/v1/Systems/system"}}};
178926f03899SShawn McCarney 
1790ed5befbdSJennifer Lee         std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
17915b4aa86bSJames Feist 
1792b49ac873SJames Feist         auto health = std::make_shared<HealthPopulate>(asyncResp);
1793b49ac873SJames Feist         health->isManagersHealth = true;
1794b49ac873SJames Feist         health->populate();
1795b49ac873SJames Feist 
1796*f97ddba7SGunnar Mills         fw_util::populateFirmwareInformation(asyncResp, fw_util::bmcPurpose,
179772d566d9SGunnar Mills                                              "FirmwareVersion", true);
17980f6b00bdSJames Feist 
17994bf2b033SGunnar Mills         getLastResetTime(asyncResp);
18004bf2b033SGunnar Mills 
180173df0db0SJames Feist         auto pids = std::make_shared<GetPIDValues>(asyncResp);
180273df0db0SJames Feist         pids->run();
1803c5d03ff4SJennifer Lee 
1804c5d03ff4SJennifer Lee         getMainChassisId(asyncResp, [](const std::string& chassisId,
1805c5d03ff4SJennifer Lee                                        const std::shared_ptr<AsyncResp> aRsp) {
1806c5d03ff4SJennifer Lee             aRsp->res.jsonValue["Links"]["ManagerForChassis@odata.count"] = 1;
1807c5d03ff4SJennifer Lee             aRsp->res.jsonValue["Links"]["ManagerForChassis"] = {
1808c5d03ff4SJennifer Lee                 {{"@odata.id", "/redfish/v1/Chassis/" + chassisId}}};
18092c0feb00SJason M. Bills             aRsp->res.jsonValue["Links"]["ManagerInChassis"] = {
18102c0feb00SJason M. Bills                 {"@odata.id", "/redfish/v1/Chassis/" + chassisId}};
1811c5d03ff4SJennifer Lee         });
18120f6b00bdSJames Feist 
18130f6b00bdSJames Feist         static bool started = false;
18140f6b00bdSJames Feist 
18150f6b00bdSJames Feist         if (!started)
18160f6b00bdSJames Feist         {
18170f6b00bdSJames Feist             crow::connections::systemBus->async_method_call(
18180f6b00bdSJames Feist                 [asyncResp](const boost::system::error_code ec,
18190f6b00bdSJames Feist                             const std::variant<double>& resp) {
18200f6b00bdSJames Feist                     if (ec)
18210f6b00bdSJames Feist                     {
18220f6b00bdSJames Feist                         BMCWEB_LOG_ERROR << "Error while getting progress";
18230f6b00bdSJames Feist                         messages::internalError(asyncResp->res);
18240f6b00bdSJames Feist                         return;
18250f6b00bdSJames Feist                     }
18260f6b00bdSJames Feist                     const double* val = std::get_if<double>(&resp);
18270f6b00bdSJames Feist                     if (val == nullptr)
18280f6b00bdSJames Feist                     {
18290f6b00bdSJames Feist                         BMCWEB_LOG_ERROR
18300f6b00bdSJames Feist                             << "Invalid response while getting progress";
18310f6b00bdSJames Feist                         messages::internalError(asyncResp->res);
18320f6b00bdSJames Feist                         return;
18330f6b00bdSJames Feist                     }
18340f6b00bdSJames Feist                     if (*val < 1.0)
18350f6b00bdSJames Feist                     {
18360f6b00bdSJames Feist                         asyncResp->res.jsonValue["Status"]["State"] =
18370f6b00bdSJames Feist                             "Starting";
18380f6b00bdSJames Feist                         started = true;
18390f6b00bdSJames Feist                     }
18400f6b00bdSJames Feist                 },
18410f6b00bdSJames Feist                 "org.freedesktop.systemd1", "/org/freedesktop/systemd1",
18420f6b00bdSJames Feist                 "org.freedesktop.DBus.Properties", "Get",
18430f6b00bdSJames Feist                 "org.freedesktop.systemd1.Manager", "Progress");
18440f6b00bdSJames Feist         }
184583ff9ab6SJames Feist     }
18465b4aa86bSJames Feist 
18475b4aa86bSJames Feist     void doPatch(crow::Response& res, const crow::Request& req,
1848cb13a392SEd Tanous                  const std::vector<std::string>&) override
18495b4aa86bSJames Feist     {
18500627a2c7SEd Tanous         std::optional<nlohmann::json> oem;
18514bfefa74SGunnar Mills         std::optional<nlohmann::json> links;
1852af5d6058SSantosh Puranik         std::optional<std::string> datetime;
185341352c24SSantosh Puranik         std::shared_ptr<AsyncResp> response = std::make_shared<AsyncResp>(res);
18540627a2c7SEd Tanous 
185541352c24SSantosh Puranik         if (!json_util::readJson(req, response->res, "Oem", oem, "DateTime",
18564bfefa74SGunnar Mills                                  datetime, "Links", links))
185783ff9ab6SJames Feist         {
185883ff9ab6SJames Feist             return;
185983ff9ab6SJames Feist         }
18600627a2c7SEd Tanous 
18610627a2c7SEd Tanous         if (oem)
186283ff9ab6SJames Feist         {
18635f2caaefSJames Feist             std::optional<nlohmann::json> openbmc;
186443b761d0SEd Tanous             if (!redfish::json_util::readJson(*oem, res, "OpenBmc", openbmc))
186583ff9ab6SJames Feist             {
186643b761d0SEd Tanous                 BMCWEB_LOG_ERROR << "Line:" << __LINE__ << ", Illegal Property "
186743b761d0SEd Tanous                                  << oem->dump();
186883ff9ab6SJames Feist                 return;
186983ff9ab6SJames Feist             }
18705f2caaefSJames Feist             if (openbmc)
187183ff9ab6SJames Feist             {
18725f2caaefSJames Feist                 std::optional<nlohmann::json> fan;
187343b761d0SEd Tanous                 if (!redfish::json_util::readJson(*openbmc, res, "Fan", fan))
187483ff9ab6SJames Feist                 {
18755f2caaefSJames Feist                     BMCWEB_LOG_ERROR << "Line:" << __LINE__
18765f2caaefSJames Feist                                      << ", Illegal Property "
18775f2caaefSJames Feist                                      << openbmc->dump();
187883ff9ab6SJames Feist                     return;
187983ff9ab6SJames Feist                 }
18805f2caaefSJames Feist                 if (fan)
188183ff9ab6SJames Feist                 {
188273df0db0SJames Feist                     auto pid = std::make_shared<SetPIDValues>(response, *fan);
188373df0db0SJames Feist                     pid->run();
188483ff9ab6SJames Feist                 }
188583ff9ab6SJames Feist             }
188683ff9ab6SJames Feist         }
18874bfefa74SGunnar Mills         if (links)
18884bfefa74SGunnar Mills         {
18894bfefa74SGunnar Mills             std::optional<nlohmann::json> activeSoftwareImage;
18904bfefa74SGunnar Mills             if (!redfish::json_util::readJson(
18914bfefa74SGunnar Mills                     *links, res, "ActiveSoftwareImage", activeSoftwareImage))
18924bfefa74SGunnar Mills             {
18934bfefa74SGunnar Mills                 return;
18944bfefa74SGunnar Mills             }
18954bfefa74SGunnar Mills             if (activeSoftwareImage)
18964bfefa74SGunnar Mills             {
18974bfefa74SGunnar Mills                 std::optional<std::string> odataId;
18984bfefa74SGunnar Mills                 if (!json_util::readJson(*activeSoftwareImage, res, "@odata.id",
18994bfefa74SGunnar Mills                                          odataId))
19004bfefa74SGunnar Mills                 {
19014bfefa74SGunnar Mills                     return;
19024bfefa74SGunnar Mills                 }
19034bfefa74SGunnar Mills 
19044bfefa74SGunnar Mills                 if (odataId)
19054bfefa74SGunnar Mills                 {
19064bfefa74SGunnar Mills                     setActiveFirmwareImage(response, std::move(*odataId));
19074bfefa74SGunnar Mills                 }
19084bfefa74SGunnar Mills             }
19094bfefa74SGunnar Mills         }
1910af5d6058SSantosh Puranik         if (datetime)
1911af5d6058SSantosh Puranik         {
1912af5d6058SSantosh Puranik             setDateTime(response, std::move(*datetime));
1913af5d6058SSantosh Puranik         }
1914af5d6058SSantosh Puranik     }
1915af5d6058SSantosh Puranik 
19164bf2b033SGunnar Mills     void getLastResetTime(std::shared_ptr<AsyncResp> aResp)
19174bf2b033SGunnar Mills     {
19184bf2b033SGunnar Mills         BMCWEB_LOG_DEBUG << "Getting Manager Last Reset Time";
19194bf2b033SGunnar Mills 
19204bf2b033SGunnar Mills         crow::connections::systemBus->async_method_call(
19214bf2b033SGunnar Mills             [aResp](const boost::system::error_code ec,
19224bf2b033SGunnar Mills                     std::variant<uint64_t>& lastResetTime) {
19234bf2b033SGunnar Mills                 if (ec)
19244bf2b033SGunnar Mills                 {
19254bf2b033SGunnar Mills                     BMCWEB_LOG_DEBUG << "D-BUS response error " << ec;
19264bf2b033SGunnar Mills                     return;
19274bf2b033SGunnar Mills                 }
19284bf2b033SGunnar Mills 
19294bf2b033SGunnar Mills                 const uint64_t* lastResetTimePtr =
19304bf2b033SGunnar Mills                     std::get_if<uint64_t>(&lastResetTime);
19314bf2b033SGunnar Mills 
19324bf2b033SGunnar Mills                 if (!lastResetTimePtr)
19334bf2b033SGunnar Mills                 {
19344bf2b033SGunnar Mills                     messages::internalError(aResp->res);
19354bf2b033SGunnar Mills                     return;
19364bf2b033SGunnar Mills                 }
19374bf2b033SGunnar Mills                 // LastRebootTime is epoch time, in milliseconds
19384bf2b033SGunnar Mills                 // https://github.com/openbmc/phosphor-dbus-interfaces/blob/7f9a128eb9296e926422ddc312c148b625890bb6/xyz/openbmc_project/State/BMC.interface.yaml#L19
19394bf2b033SGunnar Mills                 time_t lastResetTimeStamp =
19404bf2b033SGunnar Mills                     static_cast<time_t>(*lastResetTimePtr / 1000);
19414bf2b033SGunnar Mills 
19424bf2b033SGunnar Mills                 // Convert to ISO 8601 standard
19434bf2b033SGunnar Mills                 aResp->res.jsonValue["LastResetTime"] =
19444bf2b033SGunnar Mills                     crow::utility::getDateTime(lastResetTimeStamp);
19454bf2b033SGunnar Mills             },
19464bf2b033SGunnar Mills             "xyz.openbmc_project.State.BMC", "/xyz/openbmc_project/state/bmc0",
19474bf2b033SGunnar Mills             "org.freedesktop.DBus.Properties", "Get",
19484bf2b033SGunnar Mills             "xyz.openbmc_project.State.BMC", "LastRebootTime");
19494bf2b033SGunnar Mills     }
19504bf2b033SGunnar Mills 
19514bfefa74SGunnar Mills     /**
19524bfefa74SGunnar Mills      * @brief Set the running firmware image
19534bfefa74SGunnar Mills      *
19544bfefa74SGunnar Mills      * @param[i,o] aResp - Async response object
19554bfefa74SGunnar Mills      * @param[i] runningFirmwareTarget - Image to make the running image
19564bfefa74SGunnar Mills      *
19574bfefa74SGunnar Mills      * @return void
19584bfefa74SGunnar Mills      */
19594bfefa74SGunnar Mills     void setActiveFirmwareImage(std::shared_ptr<AsyncResp> aResp,
19604bfefa74SGunnar Mills                                 const std::string&& runningFirmwareTarget)
19614bfefa74SGunnar Mills     {
19624bfefa74SGunnar Mills         // Get the Id from /redfish/v1/UpdateService/FirmwareInventory/<Id>
19634bfefa74SGunnar Mills         std::string::size_type idPos = runningFirmwareTarget.rfind("/");
19644bfefa74SGunnar Mills         if (idPos == std::string::npos)
19654bfefa74SGunnar Mills         {
19664bfefa74SGunnar Mills             messages::propertyValueNotInList(aResp->res, runningFirmwareTarget,
19674bfefa74SGunnar Mills                                              "@odata.id");
19684bfefa74SGunnar Mills             BMCWEB_LOG_DEBUG << "Can't parse firmware ID!";
19694bfefa74SGunnar Mills             return;
19704bfefa74SGunnar Mills         }
19714bfefa74SGunnar Mills         idPos++;
19724bfefa74SGunnar Mills         if (idPos >= runningFirmwareTarget.size())
19734bfefa74SGunnar Mills         {
19744bfefa74SGunnar Mills             messages::propertyValueNotInList(aResp->res, runningFirmwareTarget,
19754bfefa74SGunnar Mills                                              "@odata.id");
19764bfefa74SGunnar Mills             BMCWEB_LOG_DEBUG << "Invalid firmware ID.";
19774bfefa74SGunnar Mills             return;
19784bfefa74SGunnar Mills         }
19794bfefa74SGunnar Mills         std::string firmwareId = runningFirmwareTarget.substr(idPos);
19804bfefa74SGunnar Mills 
19814bfefa74SGunnar Mills         // Make sure the image is valid before setting priority
19824bfefa74SGunnar Mills         crow::connections::systemBus->async_method_call(
19834bfefa74SGunnar Mills             [aResp, firmwareId,
19844bfefa74SGunnar Mills              runningFirmwareTarget](const boost::system::error_code ec,
19854bfefa74SGunnar Mills                                     ManagedObjectType& subtree) {
19864bfefa74SGunnar Mills                 if (ec)
19874bfefa74SGunnar Mills                 {
19884bfefa74SGunnar Mills                     BMCWEB_LOG_DEBUG << "D-Bus response error getting objects.";
19894bfefa74SGunnar Mills                     messages::internalError(aResp->res);
19904bfefa74SGunnar Mills                     return;
19914bfefa74SGunnar Mills                 }
19924bfefa74SGunnar Mills 
19934bfefa74SGunnar Mills                 if (subtree.size() == 0)
19944bfefa74SGunnar Mills                 {
19954bfefa74SGunnar Mills                     BMCWEB_LOG_DEBUG << "Can't find image!";
19964bfefa74SGunnar Mills                     messages::internalError(aResp->res);
19974bfefa74SGunnar Mills                     return;
19984bfefa74SGunnar Mills                 }
19994bfefa74SGunnar Mills 
20004bfefa74SGunnar Mills                 bool foundImage = false;
20014bfefa74SGunnar Mills                 for (auto& object : subtree)
20024bfefa74SGunnar Mills                 {
20034bfefa74SGunnar Mills                     const std::string& path =
20044bfefa74SGunnar Mills                         static_cast<const std::string&>(object.first);
20054bfefa74SGunnar Mills                     std::size_t idPos2 = path.rfind("/");
20064bfefa74SGunnar Mills 
20074bfefa74SGunnar Mills                     if (idPos2 == std::string::npos)
20084bfefa74SGunnar Mills                     {
20094bfefa74SGunnar Mills                         continue;
20104bfefa74SGunnar Mills                     }
20114bfefa74SGunnar Mills 
20124bfefa74SGunnar Mills                     idPos2++;
20134bfefa74SGunnar Mills                     if (idPos2 >= path.size())
20144bfefa74SGunnar Mills                     {
20154bfefa74SGunnar Mills                         continue;
20164bfefa74SGunnar Mills                     }
20174bfefa74SGunnar Mills 
20184bfefa74SGunnar Mills                     if (path.substr(idPos2) == firmwareId)
20194bfefa74SGunnar Mills                     {
20204bfefa74SGunnar Mills                         foundImage = true;
20214bfefa74SGunnar Mills                         break;
20224bfefa74SGunnar Mills                     }
20234bfefa74SGunnar Mills                 }
20244bfefa74SGunnar Mills 
20254bfefa74SGunnar Mills                 if (!foundImage)
20264bfefa74SGunnar Mills                 {
20274bfefa74SGunnar Mills                     messages::propertyValueNotInList(
20284bfefa74SGunnar Mills                         aResp->res, runningFirmwareTarget, "@odata.id");
20294bfefa74SGunnar Mills                     BMCWEB_LOG_DEBUG << "Invalid firmware ID.";
20304bfefa74SGunnar Mills                     return;
20314bfefa74SGunnar Mills                 }
20324bfefa74SGunnar Mills 
20334bfefa74SGunnar Mills                 BMCWEB_LOG_DEBUG << "Setting firmware version " + firmwareId +
20344bfefa74SGunnar Mills                                         " to priority 0.";
20354bfefa74SGunnar Mills 
20364bfefa74SGunnar Mills                 // Only support Immediate
20374bfefa74SGunnar Mills                 // An addition could be a Redfish Setting like
20384bfefa74SGunnar Mills                 // ActiveSoftwareImageApplyTime and support OnReset
20394bfefa74SGunnar Mills                 crow::connections::systemBus->async_method_call(
20404bfefa74SGunnar Mills                     [aResp](const boost::system::error_code ec) {
20414bfefa74SGunnar Mills                         if (ec)
20424bfefa74SGunnar Mills                         {
20434bfefa74SGunnar Mills                             BMCWEB_LOG_DEBUG << "D-Bus response error setting.";
20444bfefa74SGunnar Mills                             messages::internalError(aResp->res);
20454bfefa74SGunnar Mills                             return;
20464bfefa74SGunnar Mills                         }
20474bfefa74SGunnar Mills                         doBMCGracefulRestart(aResp);
20484bfefa74SGunnar Mills                     },
20494bfefa74SGunnar Mills 
20504bfefa74SGunnar Mills                     "xyz.openbmc_project.Software.BMC.Updater",
20514bfefa74SGunnar Mills                     "/xyz/openbmc_project/software/" + firmwareId,
20524bfefa74SGunnar Mills                     "org.freedesktop.DBus.Properties", "Set",
20534bfefa74SGunnar Mills                     "xyz.openbmc_project.Software.RedundancyPriority",
20544bfefa74SGunnar Mills                     "Priority", std::variant<uint8_t>(static_cast<uint8_t>(0)));
20554bfefa74SGunnar Mills             },
20564bfefa74SGunnar Mills             "xyz.openbmc_project.Software.BMC.Updater",
20574bfefa74SGunnar Mills             "/xyz/openbmc_project/software",
20584bfefa74SGunnar Mills             "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
20594bfefa74SGunnar Mills     }
20604bfefa74SGunnar Mills 
2061af5d6058SSantosh Puranik     void setDateTime(std::shared_ptr<AsyncResp> aResp,
2062af5d6058SSantosh Puranik                      std::string datetime) const
2063af5d6058SSantosh Puranik     {
2064af5d6058SSantosh Puranik         BMCWEB_LOG_DEBUG << "Set date time: " << datetime;
2065af5d6058SSantosh Puranik 
2066af5d6058SSantosh Puranik         std::stringstream stream(datetime);
2067af5d6058SSantosh Puranik         // Convert from ISO 8601 to boost local_time
2068af5d6058SSantosh Puranik         // (BMC only has time in UTC)
2069af5d6058SSantosh Puranik         boost::posix_time::ptime posixTime;
2070af5d6058SSantosh Puranik         boost::posix_time::ptime epoch(boost::gregorian::date(1970, 1, 1));
2071af5d6058SSantosh Puranik         // Facet gets deleted with the stringsteam
2072af5d6058SSantosh Puranik         auto ifc = std::make_unique<boost::local_time::local_time_input_facet>(
2073af5d6058SSantosh Puranik             "%Y-%m-%d %H:%M:%S%F %ZP");
2074af5d6058SSantosh Puranik         stream.imbue(std::locale(stream.getloc(), ifc.release()));
2075af5d6058SSantosh Puranik 
2076af5d6058SSantosh Puranik         boost::local_time::local_date_time ldt(
2077af5d6058SSantosh Puranik             boost::local_time::not_a_date_time);
2078af5d6058SSantosh Puranik 
2079af5d6058SSantosh Puranik         if (stream >> ldt)
2080af5d6058SSantosh Puranik         {
2081af5d6058SSantosh Puranik             posixTime = ldt.utc_time();
2082af5d6058SSantosh Puranik             boost::posix_time::time_duration dur = posixTime - epoch;
2083af5d6058SSantosh Puranik             uint64_t durMicroSecs =
2084af5d6058SSantosh Puranik                 static_cast<uint64_t>(dur.total_microseconds());
2085af5d6058SSantosh Puranik             crow::connections::systemBus->async_method_call(
2086af5d6058SSantosh Puranik                 [aResp{std::move(aResp)}, datetime{std::move(datetime)}](
2087af5d6058SSantosh Puranik                     const boost::system::error_code ec) {
2088af5d6058SSantosh Puranik                     if (ec)
2089af5d6058SSantosh Puranik                     {
2090af5d6058SSantosh Puranik                         BMCWEB_LOG_DEBUG << "Failed to set elapsed time. "
2091af5d6058SSantosh Puranik                                             "DBUS response error "
2092af5d6058SSantosh Puranik                                          << ec;
2093af5d6058SSantosh Puranik                         messages::internalError(aResp->res);
2094af5d6058SSantosh Puranik                         return;
2095af5d6058SSantosh Puranik                     }
2096af5d6058SSantosh Puranik                     aResp->res.jsonValue["DateTime"] = datetime;
2097af5d6058SSantosh Puranik                 },
2098af5d6058SSantosh Puranik                 "xyz.openbmc_project.Time.Manager",
2099af5d6058SSantosh Puranik                 "/xyz/openbmc_project/time/bmc",
2100af5d6058SSantosh Puranik                 "org.freedesktop.DBus.Properties", "Set",
2101af5d6058SSantosh Puranik                 "xyz.openbmc_project.Time.EpochTime", "Elapsed",
2102af5d6058SSantosh Puranik                 std::variant<uint64_t>(durMicroSecs));
2103af5d6058SSantosh Puranik         }
2104af5d6058SSantosh Puranik         else
2105af5d6058SSantosh Puranik         {
2106af5d6058SSantosh Puranik             messages::propertyValueFormatError(aResp->res, datetime,
2107af5d6058SSantosh Puranik                                                "DateTime");
2108af5d6058SSantosh Puranik             return;
2109af5d6058SSantosh Puranik         }
211083ff9ab6SJames Feist     }
21119c310685SBorawski.Lukasz 
21120f74e643SEd Tanous     std::string uuid;
21139c310685SBorawski.Lukasz };
21149c310685SBorawski.Lukasz 
21151abe55efSEd Tanous class ManagerCollection : public Node
21161abe55efSEd Tanous {
21179c310685SBorawski.Lukasz   public:
211852cc112dSEd Tanous     ManagerCollection(App& app) : Node(app, "/redfish/v1/Managers/")
21191abe55efSEd Tanous     {
2120a434f2bdSEd Tanous         entityPrivileges = {
2121a434f2bdSEd Tanous             {boost::beast::http::verb::get, {{"Login"}}},
2122e0d918bcSEd Tanous             {boost::beast::http::verb::head, {{"Login"}}},
2123e0d918bcSEd Tanous             {boost::beast::http::verb::patch, {{"ConfigureManager"}}},
2124e0d918bcSEd Tanous             {boost::beast::http::verb::put, {{"ConfigureManager"}}},
2125e0d918bcSEd Tanous             {boost::beast::http::verb::delete_, {{"ConfigureManager"}}},
2126e0d918bcSEd Tanous             {boost::beast::http::verb::post, {{"ConfigureManager"}}}};
21279c310685SBorawski.Lukasz     }
21289c310685SBorawski.Lukasz 
21299c310685SBorawski.Lukasz   private:
2130cb13a392SEd Tanous     void doGet(crow::Response& res, const crow::Request&,
2131cb13a392SEd Tanous                const std::vector<std::string>&) override
21321abe55efSEd Tanous     {
213383ff9ab6SJames Feist         // Collections don't include the static data added by SubRoute
213483ff9ab6SJames Feist         // because it has a duplicate entry for members
213555c7b7a2SEd Tanous         res.jsonValue["@odata.id"] = "/redfish/v1/Managers";
213655c7b7a2SEd Tanous         res.jsonValue["@odata.type"] = "#ManagerCollection.ManagerCollection";
213755c7b7a2SEd Tanous         res.jsonValue["Name"] = "Manager Collection";
213855c7b7a2SEd Tanous         res.jsonValue["Members@odata.count"] = 1;
213955c7b7a2SEd Tanous         res.jsonValue["Members"] = {
21405b4aa86bSJames Feist             {{"@odata.id", "/redfish/v1/Managers/bmc"}}};
21419c310685SBorawski.Lukasz         res.end();
21429c310685SBorawski.Lukasz     }
21439c310685SBorawski.Lukasz };
21449c310685SBorawski.Lukasz } // namespace redfish
2145