xref: /openbmc/bmcweb/features/redfish/lib/managers.hpp (revision a170f2756716f13d906678ab93351ead035e37b4)
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 
18a51fc2d2SSui Chen #include "app.hpp"
19a51fc2d2SSui Chen #include "dbus_utility.hpp"
20b49ac873SJames Feist #include "health.hpp"
21a51fc2d2SSui Chen #include "query.hpp"
22c5d03ff4SJennifer Lee #include "redfish_util.hpp"
23a51fc2d2SSui Chen #include "registries/privilege_registry.hpp"
24a51fc2d2SSui Chen #include "utils/sw_utils.hpp"
25a51fc2d2SSui Chen #include "utils/systemd_utils.hpp"
269c310685SBorawski.Lukasz 
27af5d6058SSantosh Puranik #include <boost/date_time.hpp>
281214b7e7SGunnar Mills 
29*a170f275SEd Tanous #include <algorithm>
304bfefa74SGunnar Mills #include <cstdint>
311214b7e7SGunnar Mills #include <memory>
321214b7e7SGunnar Mills #include <sstream>
33abf2add6SEd Tanous #include <variant>
345b4aa86bSJames Feist 
351abe55efSEd Tanous namespace redfish
361abe55efSEd Tanous {
37ed5befbdSJennifer Lee 
38ed5befbdSJennifer Lee /**
392a5c4407SGunnar Mills  * Function reboots the BMC.
402a5c4407SGunnar Mills  *
412a5c4407SGunnar Mills  * @param[in] asyncResp - Shared pointer for completing asynchronous calls
42ed5befbdSJennifer Lee  */
438d1b46d7Szhanghch05 inline void
448d1b46d7Szhanghch05     doBMCGracefulRestart(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
45ed5befbdSJennifer Lee {
46ed5befbdSJennifer Lee     const char* processName = "xyz.openbmc_project.State.BMC";
47ed5befbdSJennifer Lee     const char* objectPath = "/xyz/openbmc_project/state/bmc0";
48ed5befbdSJennifer Lee     const char* interfaceName = "xyz.openbmc_project.State.BMC";
49ed5befbdSJennifer Lee     const std::string& propertyValue =
50ed5befbdSJennifer Lee         "xyz.openbmc_project.State.BMC.Transition.Reboot";
51ed5befbdSJennifer Lee     const char* destProperty = "RequestedBMCTransition";
52ed5befbdSJennifer Lee 
53ed5befbdSJennifer Lee     // Create the D-Bus variant for D-Bus call.
54168e20c1SEd Tanous     dbus::utility::DbusVariantType dbusPropertyValue(propertyValue);
55ed5befbdSJennifer Lee 
56ed5befbdSJennifer Lee     crow::connections::systemBus->async_method_call(
57ed5befbdSJennifer Lee         [asyncResp](const boost::system::error_code ec) {
58ed5befbdSJennifer Lee         // Use "Set" method to set the property value.
59ed5befbdSJennifer Lee         if (ec)
60ed5befbdSJennifer Lee         {
612a5c4407SGunnar Mills             BMCWEB_LOG_DEBUG << "[Set] Bad D-Bus request error: " << ec;
62ed5befbdSJennifer Lee             messages::internalError(asyncResp->res);
63ed5befbdSJennifer Lee             return;
64ed5befbdSJennifer Lee         }
65ed5befbdSJennifer Lee 
66ed5befbdSJennifer Lee         messages::success(asyncResp->res);
67ed5befbdSJennifer Lee         },
68ed5befbdSJennifer Lee         processName, objectPath, "org.freedesktop.DBus.Properties", "Set",
69ed5befbdSJennifer Lee         interfaceName, destProperty, dbusPropertyValue);
70ed5befbdSJennifer Lee }
712a5c4407SGunnar Mills 
728d1b46d7Szhanghch05 inline void
738d1b46d7Szhanghch05     doBMCForceRestart(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
74f92af389SJayaprakash Mutyala {
75f92af389SJayaprakash Mutyala     const char* processName = "xyz.openbmc_project.State.BMC";
76f92af389SJayaprakash Mutyala     const char* objectPath = "/xyz/openbmc_project/state/bmc0";
77f92af389SJayaprakash Mutyala     const char* interfaceName = "xyz.openbmc_project.State.BMC";
78f92af389SJayaprakash Mutyala     const std::string& propertyValue =
79f92af389SJayaprakash Mutyala         "xyz.openbmc_project.State.BMC.Transition.HardReboot";
80f92af389SJayaprakash Mutyala     const char* destProperty = "RequestedBMCTransition";
81f92af389SJayaprakash Mutyala 
82f92af389SJayaprakash Mutyala     // Create the D-Bus variant for D-Bus call.
83168e20c1SEd Tanous     dbus::utility::DbusVariantType dbusPropertyValue(propertyValue);
84f92af389SJayaprakash Mutyala 
85f92af389SJayaprakash Mutyala     crow::connections::systemBus->async_method_call(
86f92af389SJayaprakash Mutyala         [asyncResp](const boost::system::error_code ec) {
87f92af389SJayaprakash Mutyala         // Use "Set" method to set the property value.
88f92af389SJayaprakash Mutyala         if (ec)
89f92af389SJayaprakash Mutyala         {
90f92af389SJayaprakash Mutyala             BMCWEB_LOG_DEBUG << "[Set] Bad D-Bus request error: " << ec;
91f92af389SJayaprakash Mutyala             messages::internalError(asyncResp->res);
92f92af389SJayaprakash Mutyala             return;
93f92af389SJayaprakash Mutyala         }
94f92af389SJayaprakash Mutyala 
95f92af389SJayaprakash Mutyala         messages::success(asyncResp->res);
96f92af389SJayaprakash Mutyala         },
97f92af389SJayaprakash Mutyala         processName, objectPath, "org.freedesktop.DBus.Properties", "Set",
98f92af389SJayaprakash Mutyala         interfaceName, destProperty, dbusPropertyValue);
99f92af389SJayaprakash Mutyala }
100f92af389SJayaprakash Mutyala 
1012a5c4407SGunnar Mills /**
1022a5c4407SGunnar Mills  * ManagerResetAction class supports the POST method for the Reset (reboot)
1032a5c4407SGunnar Mills  * action.
1042a5c4407SGunnar Mills  */
1057e860f15SJohn Edward Broadbent inline void requestRoutesManagerResetAction(App& app)
1062a5c4407SGunnar Mills {
1072a5c4407SGunnar Mills     /**
1082a5c4407SGunnar Mills      * Function handles POST method request.
1092a5c4407SGunnar Mills      * Analyzes POST body before sending Reset (Reboot) request data to D-Bus.
110f92af389SJayaprakash Mutyala      * OpenBMC supports ResetType "GracefulRestart" and "ForceRestart".
1112a5c4407SGunnar Mills      */
1127e860f15SJohn Edward Broadbent 
1137e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/Actions/Manager.Reset/")
114ed398213SEd Tanous         .privileges(redfish::privileges::postManager)
1157e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::post)(
11645ca1b86SEd Tanous             [&app](const crow::Request& req,
1177e860f15SJohn Edward Broadbent                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
1183ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
11945ca1b86SEd Tanous         {
12045ca1b86SEd Tanous             return;
12145ca1b86SEd Tanous         }
1222a5c4407SGunnar Mills         BMCWEB_LOG_DEBUG << "Post Manager Reset.";
1232a5c4407SGunnar Mills 
1242a5c4407SGunnar Mills         std::string resetType;
1252a5c4407SGunnar Mills 
12615ed6780SWilly Tu         if (!json_util::readJsonAction(req, asyncResp->res, "ResetType",
1277e860f15SJohn Edward Broadbent                                        resetType))
1282a5c4407SGunnar Mills         {
1292a5c4407SGunnar Mills             return;
1302a5c4407SGunnar Mills         }
1312a5c4407SGunnar Mills 
132f92af389SJayaprakash Mutyala         if (resetType == "GracefulRestart")
133f92af389SJayaprakash Mutyala         {
134f92af389SJayaprakash Mutyala             BMCWEB_LOG_DEBUG << "Proceeding with " << resetType;
135f92af389SJayaprakash Mutyala             doBMCGracefulRestart(asyncResp);
136f92af389SJayaprakash Mutyala             return;
137f92af389SJayaprakash Mutyala         }
1383174e4dfSEd Tanous         if (resetType == "ForceRestart")
139f92af389SJayaprakash Mutyala         {
140f92af389SJayaprakash Mutyala             BMCWEB_LOG_DEBUG << "Proceeding with " << resetType;
141f92af389SJayaprakash Mutyala             doBMCForceRestart(asyncResp);
142f92af389SJayaprakash Mutyala             return;
143f92af389SJayaprakash Mutyala         }
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;
1507e860f15SJohn Edward Broadbent         });
1512a5c4407SGunnar Mills }
152ed5befbdSJennifer Lee 
1533e40fc74SGunnar Mills /**
1543e40fc74SGunnar Mills  * ManagerResetToDefaultsAction class supports POST method for factory reset
1553e40fc74SGunnar Mills  * action.
1563e40fc74SGunnar Mills  */
1577e860f15SJohn Edward Broadbent inline void requestRoutesManagerResetToDefaultsAction(App& app)
1583e40fc74SGunnar Mills {
1593e40fc74SGunnar Mills 
1603e40fc74SGunnar Mills     /**
1613e40fc74SGunnar Mills      * Function handles ResetToDefaults POST method request.
1623e40fc74SGunnar Mills      *
1633e40fc74SGunnar Mills      * Analyzes POST body message and factory resets BMC by calling
1643e40fc74SGunnar Mills      * BMC code updater factory reset followed by a BMC reboot.
1653e40fc74SGunnar Mills      *
1663e40fc74SGunnar Mills      * BMC code updater factory reset wipes the whole BMC read-write
1673e40fc74SGunnar Mills      * filesystem which includes things like the network settings.
1683e40fc74SGunnar Mills      *
1693e40fc74SGunnar Mills      * OpenBMC only supports ResetToDefaultsType "ResetAll".
1703e40fc74SGunnar Mills      */
1717e860f15SJohn Edward Broadbent 
1727e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app,
1737e860f15SJohn Edward Broadbent                  "/redfish/v1/Managers/bmc/Actions/Manager.ResetToDefaults/")
174ed398213SEd Tanous         .privileges(redfish::privileges::postManager)
1757e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::post)(
17645ca1b86SEd Tanous             [&app](const crow::Request& req,
1777e860f15SJohn Edward Broadbent                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
1783ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
17945ca1b86SEd Tanous         {
18045ca1b86SEd Tanous             return;
18145ca1b86SEd Tanous         }
1823e40fc74SGunnar Mills         BMCWEB_LOG_DEBUG << "Post ResetToDefaults.";
1833e40fc74SGunnar Mills 
1843e40fc74SGunnar Mills         std::string resetType;
1853e40fc74SGunnar Mills 
186002d39b4SEd Tanous         if (!json_util::readJsonAction(req, asyncResp->res,
187002d39b4SEd Tanous                                        "ResetToDefaultsType", resetType))
1883e40fc74SGunnar Mills         {
1893e40fc74SGunnar Mills             BMCWEB_LOG_DEBUG << "Missing property ResetToDefaultsType.";
1903e40fc74SGunnar Mills 
191002d39b4SEd Tanous             messages::actionParameterMissing(asyncResp->res, "ResetToDefaults",
1923e40fc74SGunnar Mills                                              "ResetToDefaultsType");
1933e40fc74SGunnar Mills             return;
1943e40fc74SGunnar Mills         }
1953e40fc74SGunnar Mills 
1963e40fc74SGunnar Mills         if (resetType != "ResetAll")
1973e40fc74SGunnar Mills         {
1980fda0f12SGeorge Liu             BMCWEB_LOG_DEBUG
1990fda0f12SGeorge Liu                 << "Invalid property value for ResetToDefaultsType: "
2003e40fc74SGunnar Mills                 << resetType;
201002d39b4SEd Tanous             messages::actionParameterNotSupported(asyncResp->res, resetType,
202002d39b4SEd Tanous                                                   "ResetToDefaultsType");
2033e40fc74SGunnar Mills             return;
2043e40fc74SGunnar Mills         }
2053e40fc74SGunnar Mills 
2063e40fc74SGunnar Mills         crow::connections::systemBus->async_method_call(
2073e40fc74SGunnar Mills             [asyncResp](const boost::system::error_code ec) {
2083e40fc74SGunnar Mills             if (ec)
2093e40fc74SGunnar Mills             {
210002d39b4SEd Tanous                 BMCWEB_LOG_DEBUG << "Failed to ResetToDefaults: " << ec;
2113e40fc74SGunnar Mills                 messages::internalError(asyncResp->res);
2123e40fc74SGunnar Mills                 return;
2133e40fc74SGunnar Mills             }
2143e40fc74SGunnar Mills             // Factory Reset doesn't actually happen until a reboot
2153e40fc74SGunnar Mills             // Can't erase what the BMC is running on
2163e40fc74SGunnar Mills             doBMCGracefulRestart(asyncResp);
2173e40fc74SGunnar Mills             },
2183e40fc74SGunnar Mills             "xyz.openbmc_project.Software.BMC.Updater",
2193e40fc74SGunnar Mills             "/xyz/openbmc_project/software",
2203e40fc74SGunnar Mills             "xyz.openbmc_project.Common.FactoryReset", "Reset");
2217e860f15SJohn Edward Broadbent         });
2223e40fc74SGunnar Mills }
2233e40fc74SGunnar Mills 
2241cb1a9e6SAppaRao Puli /**
2251cb1a9e6SAppaRao Puli  * ManagerResetActionInfo derived class for delivering Manager
2261cb1a9e6SAppaRao Puli  * ResetType AllowableValues using ResetInfo schema.
2271cb1a9e6SAppaRao Puli  */
2287e860f15SJohn Edward Broadbent inline void requestRoutesManagerResetActionInfo(App& app)
2291cb1a9e6SAppaRao Puli {
2301cb1a9e6SAppaRao Puli     /**
2311cb1a9e6SAppaRao Puli      * Functions triggers appropriate requests on DBus
2321cb1a9e6SAppaRao Puli      */
2337e860f15SJohn Edward Broadbent 
2347e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/ResetActionInfo/")
235ed398213SEd Tanous         .privileges(redfish::privileges::getActionInfo)
2367e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
23745ca1b86SEd Tanous             [&app](const crow::Request& req,
2387e860f15SJohn Edward Broadbent                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
2393ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
24045ca1b86SEd Tanous         {
24145ca1b86SEd Tanous             return;
24245ca1b86SEd Tanous         }
2431476687dSEd Tanous 
2441476687dSEd Tanous         asyncResp->res.jsonValue["@odata.type"] =
2451476687dSEd Tanous             "#ActionInfo.v1_1_2.ActionInfo";
2461476687dSEd Tanous         asyncResp->res.jsonValue["@odata.id"] =
2471476687dSEd Tanous             "/redfish/v1/Managers/bmc/ResetActionInfo";
2481476687dSEd Tanous         asyncResp->res.jsonValue["Name"] = "Reset Action Info";
2491476687dSEd Tanous         asyncResp->res.jsonValue["Id"] = "ResetActionInfo";
2501476687dSEd Tanous         nlohmann::json::object_t parameter;
2511476687dSEd Tanous         parameter["Name"] = "ResetType";
2521476687dSEd Tanous         parameter["Required"] = true;
2531476687dSEd Tanous         parameter["DataType"] = "String";
2541476687dSEd Tanous 
2551476687dSEd Tanous         nlohmann::json::array_t allowableValues;
2561476687dSEd Tanous         allowableValues.push_back("GracefulRestart");
2571476687dSEd Tanous         allowableValues.push_back("ForceRestart");
2581476687dSEd Tanous         parameter["AllowableValues"] = std::move(allowableValues);
2591476687dSEd Tanous 
2601476687dSEd Tanous         nlohmann::json::array_t parameters;
2611476687dSEd Tanous         parameters.push_back(std::move(parameter));
2621476687dSEd Tanous 
2631476687dSEd Tanous         asyncResp->res.jsonValue["Parameters"] = std::move(parameters);
2647e860f15SJohn Edward Broadbent         });
2651cb1a9e6SAppaRao Puli }
2661cb1a9e6SAppaRao Puli 
2675b4aa86bSJames Feist static constexpr const char* objectManagerIface =
2685b4aa86bSJames Feist     "org.freedesktop.DBus.ObjectManager";
2695b4aa86bSJames Feist static constexpr const char* pidConfigurationIface =
2705b4aa86bSJames Feist     "xyz.openbmc_project.Configuration.Pid";
2715b4aa86bSJames Feist static constexpr const char* pidZoneConfigurationIface =
2725b4aa86bSJames Feist     "xyz.openbmc_project.Configuration.Pid.Zone";
273b7a08d04SJames Feist static constexpr const char* stepwiseConfigurationIface =
274b7a08d04SJames Feist     "xyz.openbmc_project.Configuration.Stepwise";
27573df0db0SJames Feist static constexpr const char* thermalModeIface =
27673df0db0SJames Feist     "xyz.openbmc_project.Control.ThermalMode";
2779c310685SBorawski.Lukasz 
2788d1b46d7Szhanghch05 inline void
2798d1b46d7Szhanghch05     asyncPopulatePid(const std::string& connection, const std::string& path,
28073df0db0SJames Feist                      const std::string& currentProfile,
28173df0db0SJames Feist                      const std::vector<std::string>& supportedProfiles,
2828d1b46d7Szhanghch05                      const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
2835b4aa86bSJames Feist {
2845b4aa86bSJames Feist 
2855b4aa86bSJames Feist     crow::connections::systemBus->async_method_call(
28673df0db0SJames Feist         [asyncResp, currentProfile, supportedProfiles](
28773df0db0SJames Feist             const boost::system::error_code ec,
2885b4aa86bSJames Feist             const dbus::utility::ManagedObjectType& managedObj) {
2895b4aa86bSJames Feist         if (ec)
2905b4aa86bSJames Feist         {
2915b4aa86bSJames Feist             BMCWEB_LOG_ERROR << ec;
2925b4aa86bSJames Feist             asyncResp->res.jsonValue.clear();
293f12894f8SJason M. Bills             messages::internalError(asyncResp->res);
2945b4aa86bSJames Feist             return;
2955b4aa86bSJames Feist         }
2965b4aa86bSJames Feist         nlohmann::json& configRoot =
2975b4aa86bSJames Feist             asyncResp->res.jsonValue["Oem"]["OpenBmc"]["Fan"];
2985b4aa86bSJames Feist         nlohmann::json& fans = configRoot["FanControllers"];
2995b4aa86bSJames Feist         fans["@odata.type"] = "#OemManager.FanControllers";
3000fda0f12SGeorge Liu         fans["@odata.id"] =
3010fda0f12SGeorge Liu             "/redfish/v1/Managers/bmc#/Oem/OpenBmc/Fan/FanControllers";
3025b4aa86bSJames Feist 
3035b4aa86bSJames Feist         nlohmann::json& pids = configRoot["PidControllers"];
3045b4aa86bSJames Feist         pids["@odata.type"] = "#OemManager.PidControllers";
3055b4aa86bSJames Feist         pids["@odata.id"] =
3065b4aa86bSJames Feist             "/redfish/v1/Managers/bmc#/Oem/OpenBmc/Fan/PidControllers";
3075b4aa86bSJames Feist 
308b7a08d04SJames Feist         nlohmann::json& stepwise = configRoot["StepwiseControllers"];
309b7a08d04SJames Feist         stepwise["@odata.type"] = "#OemManager.StepwiseControllers";
310b7a08d04SJames Feist         stepwise["@odata.id"] =
311b7a08d04SJames Feist             "/redfish/v1/Managers/bmc#/Oem/OpenBmc/Fan/StepwiseControllers";
312b7a08d04SJames Feist 
3135b4aa86bSJames Feist         nlohmann::json& zones = configRoot["FanZones"];
3145b4aa86bSJames Feist         zones["@odata.id"] =
3155b4aa86bSJames Feist             "/redfish/v1/Managers/bmc#/Oem/OpenBmc/Fan/FanZones";
3165b4aa86bSJames Feist         zones["@odata.type"] = "#OemManager.FanZones";
317002d39b4SEd Tanous         configRoot["@odata.id"] = "/redfish/v1/Managers/bmc#/Oem/OpenBmc/Fan";
3185b4aa86bSJames Feist         configRoot["@odata.type"] = "#OemManager.Fan";
31973df0db0SJames Feist         configRoot["Profile@Redfish.AllowableValues"] = supportedProfiles;
32073df0db0SJames Feist 
32173df0db0SJames Feist         if (!currentProfile.empty())
32273df0db0SJames Feist         {
32373df0db0SJames Feist             configRoot["Profile"] = currentProfile;
32473df0db0SJames Feist         }
32573df0db0SJames Feist         BMCWEB_LOG_ERROR << "profile = " << currentProfile << " !";
3265b4aa86bSJames Feist 
3275b4aa86bSJames Feist         for (const auto& pathPair : managedObj)
3285b4aa86bSJames Feist         {
3295b4aa86bSJames Feist             for (const auto& intfPair : pathPair.second)
3305b4aa86bSJames Feist             {
3315b4aa86bSJames Feist                 if (intfPair.first != pidConfigurationIface &&
332b7a08d04SJames Feist                     intfPair.first != pidZoneConfigurationIface &&
333b7a08d04SJames Feist                     intfPair.first != stepwiseConfigurationIface)
3345b4aa86bSJames Feist                 {
3355b4aa86bSJames Feist                     continue;
3365b4aa86bSJames Feist                 }
33773df0db0SJames Feist 
338711ac7a9SEd Tanous                 std::string name;
339711ac7a9SEd Tanous 
340711ac7a9SEd Tanous                 for (const std::pair<std::string,
341002d39b4SEd Tanous                                      dbus::utility::DbusVariantType>& propPair :
342002d39b4SEd Tanous                      intfPair.second)
343711ac7a9SEd Tanous                 {
344711ac7a9SEd Tanous                     if (propPair.first == "Name")
345711ac7a9SEd Tanous                     {
3465b4aa86bSJames Feist                         const std::string* namePtr =
347711ac7a9SEd Tanous                             std::get_if<std::string>(&propPair.second);
3485b4aa86bSJames Feist                         if (namePtr == nullptr)
3495b4aa86bSJames Feist                         {
3505b4aa86bSJames Feist                             BMCWEB_LOG_ERROR << "Pid Name Field illegal";
351b7a08d04SJames Feist                             messages::internalError(asyncResp->res);
3525b4aa86bSJames Feist                             return;
3535b4aa86bSJames Feist                         }
354db697703SWilly Tu                         name = *namePtr;
3555b4aa86bSJames Feist                         dbus::utility::escapePathForDbus(name);
356711ac7a9SEd Tanous                     }
357711ac7a9SEd Tanous                     else if (propPair.first == "Profiles")
35873df0db0SJames Feist                     {
35973df0db0SJames Feist                         const std::vector<std::string>* profiles =
36073df0db0SJames Feist                             std::get_if<std::vector<std::string>>(
361711ac7a9SEd Tanous                                 &propPair.second);
36273df0db0SJames Feist                         if (profiles == nullptr)
36373df0db0SJames Feist                         {
364002d39b4SEd Tanous                             BMCWEB_LOG_ERROR << "Pid Profiles Field illegal";
36573df0db0SJames Feist                             messages::internalError(asyncResp->res);
36673df0db0SJames Feist                             return;
36773df0db0SJames Feist                         }
36873df0db0SJames Feist                         if (std::find(profiles->begin(), profiles->end(),
36973df0db0SJames Feist                                       currentProfile) == profiles->end())
37073df0db0SJames Feist                         {
37173df0db0SJames Feist                             BMCWEB_LOG_INFO
372002d39b4SEd Tanous                                 << name << " not supported in current profile";
37373df0db0SJames Feist                             continue;
37473df0db0SJames Feist                         }
37573df0db0SJames Feist                     }
376711ac7a9SEd Tanous                 }
377b7a08d04SJames Feist                 nlohmann::json* config = nullptr;
378c33a90ecSJames Feist                 const std::string* classPtr = nullptr;
379711ac7a9SEd Tanous 
380711ac7a9SEd Tanous                 for (const std::pair<std::string,
381002d39b4SEd Tanous                                      dbus::utility::DbusVariantType>& propPair :
382002d39b4SEd Tanous                      intfPair.second)
383c33a90ecSJames Feist                 {
384727dc83fSLei YU                     if (propPair.first == "Class")
385711ac7a9SEd Tanous                     {
386002d39b4SEd Tanous                         classPtr = std::get_if<std::string>(&propPair.second);
387711ac7a9SEd Tanous                     }
388c33a90ecSJames Feist                 }
389c33a90ecSJames Feist 
3905b4aa86bSJames Feist                 if (intfPair.first == pidZoneConfigurationIface)
3915b4aa86bSJames Feist                 {
3925b4aa86bSJames Feist                     std::string chassis;
393002d39b4SEd Tanous                     if (!dbus::utility::getNthStringFromPath(pathPair.first.str,
394002d39b4SEd Tanous                                                              5, chassis))
3955b4aa86bSJames Feist                     {
3965b4aa86bSJames Feist                         chassis = "#IllegalValue";
3975b4aa86bSJames Feist                     }
3985b4aa86bSJames Feist                     nlohmann::json& zone = zones[name];
3995b4aa86bSJames Feist                     zone["Chassis"] = {
4005b4aa86bSJames Feist                         {"@odata.id", "/redfish/v1/Chassis/" + chassis}};
4010fda0f12SGeorge Liu                     zone["@odata.id"] =
4020fda0f12SGeorge Liu                         "/redfish/v1/Managers/bmc#/Oem/OpenBmc/Fan/FanZones/" +
4035b4aa86bSJames Feist                         name;
4045b4aa86bSJames Feist                     zone["@odata.type"] = "#OemManager.FanZone";
405b7a08d04SJames Feist                     config = &zone;
4065b4aa86bSJames Feist                 }
4075b4aa86bSJames Feist 
408b7a08d04SJames Feist                 else if (intfPair.first == stepwiseConfigurationIface)
4095b4aa86bSJames Feist                 {
410c33a90ecSJames Feist                     if (classPtr == nullptr)
411c33a90ecSJames Feist                     {
412c33a90ecSJames Feist                         BMCWEB_LOG_ERROR << "Pid Class Field illegal";
413c33a90ecSJames Feist                         messages::internalError(asyncResp->res);
414c33a90ecSJames Feist                         return;
415c33a90ecSJames Feist                     }
416c33a90ecSJames Feist 
417b7a08d04SJames Feist                     nlohmann::json& controller = stepwise[name];
418b7a08d04SJames Feist                     config = &controller;
4195b4aa86bSJames Feist 
420b7a08d04SJames Feist                     controller["@odata.id"] =
4210fda0f12SGeorge Liu                         "/redfish/v1/Managers/bmc#/Oem/OpenBmc/Fan/StepwiseControllers/" +
422271584abSEd Tanous                         name;
423b7a08d04SJames Feist                     controller["@odata.type"] =
424b7a08d04SJames Feist                         "#OemManager.StepwiseController";
425b7a08d04SJames Feist 
426c33a90ecSJames Feist                     controller["Direction"] = *classPtr;
4275b4aa86bSJames Feist                 }
4285b4aa86bSJames Feist 
4295b4aa86bSJames Feist                 // pid and fans are off the same configuration
430b7a08d04SJames Feist                 else if (intfPair.first == pidConfigurationIface)
4315b4aa86bSJames Feist                 {
432c33a90ecSJames Feist 
4335b4aa86bSJames Feist                     if (classPtr == nullptr)
4345b4aa86bSJames Feist                     {
4355b4aa86bSJames Feist                         BMCWEB_LOG_ERROR << "Pid Class Field illegal";
436a08b46ccSJason M. Bills                         messages::internalError(asyncResp->res);
4375b4aa86bSJames Feist                         return;
4385b4aa86bSJames Feist                     }
4395b4aa86bSJames Feist                     bool isFan = *classPtr == "fan";
440002d39b4SEd Tanous                     nlohmann::json& element = isFan ? fans[name] : pids[name];
441b7a08d04SJames Feist                     config = &element;
4425b4aa86bSJames Feist                     if (isFan)
4435b4aa86bSJames Feist                     {
4445b4aa86bSJames Feist                         element["@odata.id"] =
4450fda0f12SGeorge Liu                             "/redfish/v1/Managers/bmc#/Oem/OpenBmc/Fan/FanControllers/" +
446271584abSEd Tanous                             name;
447002d39b4SEd Tanous                         element["@odata.type"] = "#OemManager.FanController";
4485b4aa86bSJames Feist                     }
4495b4aa86bSJames Feist                     else
4505b4aa86bSJames Feist                     {
4515b4aa86bSJames Feist                         element["@odata.id"] =
4520fda0f12SGeorge Liu                             "/redfish/v1/Managers/bmc#/Oem/OpenBmc/Fan/PidControllers/" +
453271584abSEd Tanous                             name;
454002d39b4SEd Tanous                         element["@odata.type"] = "#OemManager.PidController";
4555b4aa86bSJames Feist                     }
456b7a08d04SJames Feist                 }
457b7a08d04SJames Feist                 else
458b7a08d04SJames Feist                 {
459b7a08d04SJames Feist                     BMCWEB_LOG_ERROR << "Unexpected configuration";
460b7a08d04SJames Feist                     messages::internalError(asyncResp->res);
461b7a08d04SJames Feist                     return;
462b7a08d04SJames Feist                 }
463b7a08d04SJames Feist 
464b7a08d04SJames Feist                 // used for making maps out of 2 vectors
465b7a08d04SJames Feist                 const std::vector<double>* keys = nullptr;
466b7a08d04SJames Feist                 const std::vector<double>* values = nullptr;
467b7a08d04SJames Feist 
468b7a08d04SJames Feist                 for (const auto& propertyPair : intfPair.second)
469b7a08d04SJames Feist                 {
470b7a08d04SJames Feist                     if (propertyPair.first == "Type" ||
471b7a08d04SJames Feist                         propertyPair.first == "Class" ||
472b7a08d04SJames Feist                         propertyPair.first == "Name")
473b7a08d04SJames Feist                     {
474b7a08d04SJames Feist                         continue;
475b7a08d04SJames Feist                     }
476b7a08d04SJames Feist 
477b7a08d04SJames Feist                     // zones
478b7a08d04SJames Feist                     if (intfPair.first == pidZoneConfigurationIface)
479b7a08d04SJames Feist                     {
480b7a08d04SJames Feist                         const double* ptr =
481abf2add6SEd Tanous                             std::get_if<double>(&propertyPair.second);
482b7a08d04SJames Feist                         if (ptr == nullptr)
483b7a08d04SJames Feist                         {
484b7a08d04SJames Feist                             BMCWEB_LOG_ERROR << "Field Illegal "
485b7a08d04SJames Feist                                              << propertyPair.first;
486b7a08d04SJames Feist                             messages::internalError(asyncResp->res);
487b7a08d04SJames Feist                             return;
488b7a08d04SJames Feist                         }
489b7a08d04SJames Feist                         (*config)[propertyPair.first] = *ptr;
490b7a08d04SJames Feist                     }
491b7a08d04SJames Feist 
492b7a08d04SJames Feist                     if (intfPair.first == stepwiseConfigurationIface)
493b7a08d04SJames Feist                     {
494b7a08d04SJames Feist                         if (propertyPair.first == "Reading" ||
495b7a08d04SJames Feist                             propertyPair.first == "Output")
496b7a08d04SJames Feist                         {
497b7a08d04SJames Feist                             const std::vector<double>* ptr =
498abf2add6SEd Tanous                                 std::get_if<std::vector<double>>(
499b7a08d04SJames Feist                                     &propertyPair.second);
500b7a08d04SJames Feist 
501b7a08d04SJames Feist                             if (ptr == nullptr)
502b7a08d04SJames Feist                             {
503b7a08d04SJames Feist                                 BMCWEB_LOG_ERROR << "Field Illegal "
504b7a08d04SJames Feist                                                  << propertyPair.first;
505b7a08d04SJames Feist                                 messages::internalError(asyncResp->res);
506b7a08d04SJames Feist                                 return;
507b7a08d04SJames Feist                             }
508b7a08d04SJames Feist 
509b7a08d04SJames Feist                             if (propertyPair.first == "Reading")
510b7a08d04SJames Feist                             {
511b7a08d04SJames Feist                                 keys = ptr;
512b7a08d04SJames Feist                             }
513b7a08d04SJames Feist                             else
514b7a08d04SJames Feist                             {
515b7a08d04SJames Feist                                 values = ptr;
516b7a08d04SJames Feist                             }
517e662eae8SEd Tanous                             if (keys != nullptr && values != nullptr)
518b7a08d04SJames Feist                             {
519b7a08d04SJames Feist                                 if (keys->size() != values->size())
520b7a08d04SJames Feist                                 {
521b7a08d04SJames Feist                                     BMCWEB_LOG_ERROR
5220fda0f12SGeorge Liu                                         << "Reading and Output size don't match ";
523b7a08d04SJames Feist                                     messages::internalError(asyncResp->res);
524b7a08d04SJames Feist                                     return;
525b7a08d04SJames Feist                                 }
526b7a08d04SJames Feist                                 nlohmann::json& steps = (*config)["Steps"];
527b7a08d04SJames Feist                                 steps = nlohmann::json::array();
528b7a08d04SJames Feist                                 for (size_t ii = 0; ii < keys->size(); ii++)
529b7a08d04SJames Feist                                 {
5301476687dSEd Tanous                                     nlohmann::json::object_t step;
5311476687dSEd Tanous                                     step["Target"] = (*keys)[ii];
5321476687dSEd Tanous                                     step["Output"] = (*values)[ii];
5331476687dSEd Tanous                                     steps.push_back(std::move(step));
534b7a08d04SJames Feist                                 }
535b7a08d04SJames Feist                             }
536b7a08d04SJames Feist                         }
537b7a08d04SJames Feist                         if (propertyPair.first == "NegativeHysteresis" ||
538b7a08d04SJames Feist                             propertyPair.first == "PositiveHysteresis")
539b7a08d04SJames Feist                         {
540b7a08d04SJames Feist                             const double* ptr =
541abf2add6SEd Tanous                                 std::get_if<double>(&propertyPair.second);
542b7a08d04SJames Feist                             if (ptr == nullptr)
543b7a08d04SJames Feist                             {
544b7a08d04SJames Feist                                 BMCWEB_LOG_ERROR << "Field Illegal "
545b7a08d04SJames Feist                                                  << propertyPair.first;
546b7a08d04SJames Feist                                 messages::internalError(asyncResp->res);
547b7a08d04SJames Feist                                 return;
548b7a08d04SJames Feist                             }
549b7a08d04SJames Feist                             (*config)[propertyPair.first] = *ptr;
550b7a08d04SJames Feist                         }
551b7a08d04SJames Feist                     }
552b7a08d04SJames Feist 
553b7a08d04SJames Feist                     // pid and fans are off the same configuration
554b7a08d04SJames Feist                     if (intfPair.first == pidConfigurationIface ||
555b7a08d04SJames Feist                         intfPair.first == stepwiseConfigurationIface)
556b7a08d04SJames Feist                     {
5575b4aa86bSJames Feist 
5585b4aa86bSJames Feist                         if (propertyPair.first == "Zones")
5595b4aa86bSJames Feist                         {
5605b4aa86bSJames Feist                             const std::vector<std::string>* inputs =
561abf2add6SEd Tanous                                 std::get_if<std::vector<std::string>>(
5621b6b96c5SEd Tanous                                     &propertyPair.second);
5635b4aa86bSJames Feist 
5645b4aa86bSJames Feist                             if (inputs == nullptr)
5655b4aa86bSJames Feist                             {
566002d39b4SEd Tanous                                 BMCWEB_LOG_ERROR << "Zones Pid Field Illegal";
567a08b46ccSJason M. Bills                                 messages::internalError(asyncResp->res);
5685b4aa86bSJames Feist                                 return;
5695b4aa86bSJames Feist                             }
570b7a08d04SJames Feist                             auto& data = (*config)[propertyPair.first];
5715b4aa86bSJames Feist                             data = nlohmann::json::array();
5725b4aa86bSJames Feist                             for (std::string itemCopy : *inputs)
5735b4aa86bSJames Feist                             {
5745b4aa86bSJames Feist                                 dbus::utility::escapePathForDbus(itemCopy);
5751476687dSEd Tanous                                 nlohmann::json::object_t input;
5761476687dSEd Tanous                                 input["@odata.id"] =
5770fda0f12SGeorge Liu                                     "/redfish/v1/Managers/bmc#/Oem/OpenBmc/Fan/FanZones/" +
5781476687dSEd Tanous                                     itemCopy;
5791476687dSEd Tanous                                 data.push_back(std::move(input));
5805b4aa86bSJames Feist                             }
5815b4aa86bSJames Feist                         }
5825b4aa86bSJames Feist                         // todo(james): may never happen, but this
5835b4aa86bSJames Feist                         // assumes configuration data referenced in the
5845b4aa86bSJames Feist                         // PID config is provided by the same daemon, we
5855b4aa86bSJames Feist                         // could add another loop to cover all cases,
5865b4aa86bSJames Feist                         // but I'm okay kicking this can down the road a
5875b4aa86bSJames Feist                         // bit
5885b4aa86bSJames Feist 
5895b4aa86bSJames Feist                         else if (propertyPair.first == "Inputs" ||
5905b4aa86bSJames Feist                                  propertyPair.first == "Outputs")
5915b4aa86bSJames Feist                         {
592b7a08d04SJames Feist                             auto& data = (*config)[propertyPair.first];
5935b4aa86bSJames Feist                             const std::vector<std::string>* inputs =
594abf2add6SEd Tanous                                 std::get_if<std::vector<std::string>>(
5951b6b96c5SEd Tanous                                     &propertyPair.second);
5965b4aa86bSJames Feist 
5975b4aa86bSJames Feist                             if (inputs == nullptr)
5985b4aa86bSJames Feist                             {
5995b4aa86bSJames Feist                                 BMCWEB_LOG_ERROR << "Field Illegal "
6005b4aa86bSJames Feist                                                  << propertyPair.first;
601f12894f8SJason M. Bills                                 messages::internalError(asyncResp->res);
6025b4aa86bSJames Feist                                 return;
6035b4aa86bSJames Feist                             }
6045b4aa86bSJames Feist                             data = *inputs;
605b943aaefSJames Feist                         }
606b943aaefSJames Feist                         else if (propertyPair.first == "SetPointOffset")
607b943aaefSJames Feist                         {
608b943aaefSJames Feist                             const std::string* ptr =
609002d39b4SEd Tanous                                 std::get_if<std::string>(&propertyPair.second);
610b943aaefSJames Feist 
611b943aaefSJames Feist                             if (ptr == nullptr)
612b943aaefSJames Feist                             {
613b943aaefSJames Feist                                 BMCWEB_LOG_ERROR << "Field Illegal "
614b943aaefSJames Feist                                                  << propertyPair.first;
615b943aaefSJames Feist                                 messages::internalError(asyncResp->res);
616b943aaefSJames Feist                                 return;
617b943aaefSJames Feist                             }
618b943aaefSJames Feist                             // translate from dbus to redfish
619b943aaefSJames Feist                             if (*ptr == "WarningHigh")
620b943aaefSJames Feist                             {
621b943aaefSJames Feist                                 (*config)["SetPointOffset"] =
622b943aaefSJames Feist                                     "UpperThresholdNonCritical";
623b943aaefSJames Feist                             }
624b943aaefSJames Feist                             else if (*ptr == "WarningLow")
625b943aaefSJames Feist                             {
626b943aaefSJames Feist                                 (*config)["SetPointOffset"] =
627b943aaefSJames Feist                                     "LowerThresholdNonCritical";
628b943aaefSJames Feist                             }
629b943aaefSJames Feist                             else if (*ptr == "CriticalHigh")
630b943aaefSJames Feist                             {
631b943aaefSJames Feist                                 (*config)["SetPointOffset"] =
632b943aaefSJames Feist                                     "UpperThresholdCritical";
633b943aaefSJames Feist                             }
634b943aaefSJames Feist                             else if (*ptr == "CriticalLow")
635b943aaefSJames Feist                             {
636b943aaefSJames Feist                                 (*config)["SetPointOffset"] =
637b943aaefSJames Feist                                     "LowerThresholdCritical";
638b943aaefSJames Feist                             }
639b943aaefSJames Feist                             else
640b943aaefSJames Feist                             {
641002d39b4SEd Tanous                                 BMCWEB_LOG_ERROR << "Value Illegal " << *ptr;
642b943aaefSJames Feist                                 messages::internalError(asyncResp->res);
643b943aaefSJames Feist                                 return;
644b943aaefSJames Feist                             }
645b943aaefSJames Feist                         }
646b943aaefSJames Feist                         // doubles
647002d39b4SEd Tanous                         else if (propertyPair.first == "FFGainCoefficient" ||
6485b4aa86bSJames Feist                                  propertyPair.first == "FFOffCoefficient" ||
6495b4aa86bSJames Feist                                  propertyPair.first == "ICoefficient" ||
6505b4aa86bSJames Feist                                  propertyPair.first == "ILimitMax" ||
6515b4aa86bSJames Feist                                  propertyPair.first == "ILimitMin" ||
652002d39b4SEd Tanous                                  propertyPair.first == "PositiveHysteresis" ||
653002d39b4SEd Tanous                                  propertyPair.first == "NegativeHysteresis" ||
6545b4aa86bSJames Feist                                  propertyPair.first == "OutLimitMax" ||
6555b4aa86bSJames Feist                                  propertyPair.first == "OutLimitMin" ||
6565b4aa86bSJames Feist                                  propertyPair.first == "PCoefficient" ||
6577625cb81SJames Feist                                  propertyPair.first == "SetPoint" ||
6585b4aa86bSJames Feist                                  propertyPair.first == "SlewNeg" ||
6595b4aa86bSJames Feist                                  propertyPair.first == "SlewPos")
6605b4aa86bSJames Feist                         {
6615b4aa86bSJames Feist                             const double* ptr =
662abf2add6SEd Tanous                                 std::get_if<double>(&propertyPair.second);
6635b4aa86bSJames Feist                             if (ptr == nullptr)
6645b4aa86bSJames Feist                             {
6655b4aa86bSJames Feist                                 BMCWEB_LOG_ERROR << "Field Illegal "
6665b4aa86bSJames Feist                                                  << propertyPair.first;
667f12894f8SJason M. Bills                                 messages::internalError(asyncResp->res);
6685b4aa86bSJames Feist                                 return;
6695b4aa86bSJames Feist                             }
670b7a08d04SJames Feist                             (*config)[propertyPair.first] = *ptr;
6715b4aa86bSJames Feist                         }
6725b4aa86bSJames Feist                     }
6735b4aa86bSJames Feist                 }
6745b4aa86bSJames Feist             }
6755b4aa86bSJames Feist         }
6765b4aa86bSJames Feist         },
6775b4aa86bSJames Feist         connection, path, objectManagerIface, "GetManagedObjects");
6785b4aa86bSJames Feist }
679ca537928SJennifer Lee 
68083ff9ab6SJames Feist enum class CreatePIDRet
68183ff9ab6SJames Feist {
68283ff9ab6SJames Feist     fail,
68383ff9ab6SJames Feist     del,
68483ff9ab6SJames Feist     patch
68583ff9ab6SJames Feist };
68683ff9ab6SJames Feist 
6878d1b46d7Szhanghch05 inline bool
6888d1b46d7Szhanghch05     getZonesFromJsonReq(const std::shared_ptr<bmcweb::AsyncResp>& response,
6895f2caaefSJames Feist                         std::vector<nlohmann::json>& config,
6905f2caaefSJames Feist                         std::vector<std::string>& zones)
6915f2caaefSJames Feist {
692b6baeaa4SJames Feist     if (config.empty())
693b6baeaa4SJames Feist     {
694b6baeaa4SJames Feist         BMCWEB_LOG_ERROR << "Empty Zones";
6951668ce6dSEd Tanous         messages::propertyValueFormatError(response->res, "[]", "Zones");
696b6baeaa4SJames Feist         return false;
697b6baeaa4SJames Feist     }
6985f2caaefSJames Feist     for (auto& odata : config)
6995f2caaefSJames Feist     {
7005f2caaefSJames Feist         std::string path;
7015f2caaefSJames Feist         if (!redfish::json_util::readJson(odata, response->res, "@odata.id",
7025f2caaefSJames Feist                                           path))
7035f2caaefSJames Feist         {
7045f2caaefSJames Feist             return false;
7055f2caaefSJames Feist         }
7065f2caaefSJames Feist         std::string input;
70761adbda3SJames Feist 
70861adbda3SJames Feist         // 8 below comes from
70961adbda3SJames Feist         // /redfish/v1/Managers/bmc#/Oem/OpenBmc/Fan/FanZones/Left
71061adbda3SJames Feist         //     0    1     2      3    4    5      6     7      8
71161adbda3SJames Feist         if (!dbus::utility::getNthStringFromPath(path, 8, input))
7125f2caaefSJames Feist         {
7135f2caaefSJames Feist             BMCWEB_LOG_ERROR << "Got invalid path " << path;
7145f2caaefSJames Feist             BMCWEB_LOG_ERROR << "Illegal Type Zones";
7155f2caaefSJames Feist             messages::propertyValueFormatError(response->res, odata.dump(),
7165f2caaefSJames Feist                                                "Zones");
7175f2caaefSJames Feist             return false;
7185f2caaefSJames Feist         }
719*a170f275SEd Tanous         std::replace(input.begin(), input.end(), '_', ' ');
7205f2caaefSJames Feist         zones.emplace_back(std::move(input));
7215f2caaefSJames Feist     }
7225f2caaefSJames Feist     return true;
7235f2caaefSJames Feist }
7245f2caaefSJames Feist 
725711ac7a9SEd Tanous inline const dbus::utility::ManagedObjectType::value_type*
72673df0db0SJames Feist     findChassis(const dbus::utility::ManagedObjectType& managedObj,
727b6baeaa4SJames Feist                 const std::string& value, std::string& chassis)
728b6baeaa4SJames Feist {
729b6baeaa4SJames Feist     BMCWEB_LOG_DEBUG << "Find Chassis: " << value << "\n";
730b6baeaa4SJames Feist 
731*a170f275SEd Tanous     std::string escaped = value;
732*a170f275SEd Tanous     std::replace(escaped.begin(), escaped.end(), '_', ' ');
733b6baeaa4SJames Feist     escaped = "/" + escaped;
734002d39b4SEd Tanous     auto it = std::find_if(managedObj.begin(), managedObj.end(),
735002d39b4SEd Tanous                            [&escaped](const auto& obj) {
736b6baeaa4SJames Feist         if (boost::algorithm::ends_with(obj.first.str, escaped))
737b6baeaa4SJames Feist         {
738b6baeaa4SJames Feist             BMCWEB_LOG_DEBUG << "Matched " << obj.first.str << "\n";
739b6baeaa4SJames Feist             return true;
740b6baeaa4SJames Feist         }
741b6baeaa4SJames Feist         return false;
742b6baeaa4SJames Feist     });
743b6baeaa4SJames Feist 
744b6baeaa4SJames Feist     if (it == managedObj.end())
745b6baeaa4SJames Feist     {
74673df0db0SJames Feist         return nullptr;
747b6baeaa4SJames Feist     }
748b6baeaa4SJames Feist     // 5 comes from <chassis-name> being the 5th element
749b6baeaa4SJames Feist     // /xyz/openbmc_project/inventory/system/chassis/<chassis-name>
75073df0db0SJames Feist     if (dbus::utility::getNthStringFromPath(it->first.str, 5, chassis))
75173df0db0SJames Feist     {
75273df0db0SJames Feist         return &(*it);
75373df0db0SJames Feist     }
75473df0db0SJames Feist 
75573df0db0SJames Feist     return nullptr;
756b6baeaa4SJames Feist }
757b6baeaa4SJames Feist 
75823a21a1cSEd Tanous inline CreatePIDRet createPidInterface(
7598d1b46d7Szhanghch05     const std::shared_ptr<bmcweb::AsyncResp>& response, const std::string& type,
760b5a76932SEd Tanous     const nlohmann::json::iterator& it, const std::string& path,
76183ff9ab6SJames Feist     const dbus::utility::ManagedObjectType& managedObj, bool createNewObject,
762b9d36b47SEd Tanous     dbus::utility::DBusPropertiesMap& output, std::string& chassis,
763b9d36b47SEd Tanous     const std::string& profile)
76483ff9ab6SJames Feist {
76583ff9ab6SJames Feist 
7665f2caaefSJames Feist     // common deleter
767b6baeaa4SJames Feist     if (it.value() == nullptr)
7685f2caaefSJames Feist     {
7695f2caaefSJames Feist         std::string iface;
7705f2caaefSJames Feist         if (type == "PidControllers" || type == "FanControllers")
7715f2caaefSJames Feist         {
7725f2caaefSJames Feist             iface = pidConfigurationIface;
7735f2caaefSJames Feist         }
7745f2caaefSJames Feist         else if (type == "FanZones")
7755f2caaefSJames Feist         {
7765f2caaefSJames Feist             iface = pidZoneConfigurationIface;
7775f2caaefSJames Feist         }
7785f2caaefSJames Feist         else if (type == "StepwiseControllers")
7795f2caaefSJames Feist         {
7805f2caaefSJames Feist             iface = stepwiseConfigurationIface;
7815f2caaefSJames Feist         }
7825f2caaefSJames Feist         else
7835f2caaefSJames Feist         {
784a0744d38SGunnar Mills             BMCWEB_LOG_ERROR << "Illegal Type " << type;
7855f2caaefSJames Feist             messages::propertyUnknown(response->res, type);
7865f2caaefSJames Feist             return CreatePIDRet::fail;
7875f2caaefSJames Feist         }
7886ee7f774SJames Feist 
7896ee7f774SJames Feist         BMCWEB_LOG_DEBUG << "del " << path << " " << iface << "\n";
7905f2caaefSJames Feist         // delete interface
7915f2caaefSJames Feist         crow::connections::systemBus->async_method_call(
7925f2caaefSJames Feist             [response, path](const boost::system::error_code ec) {
7935f2caaefSJames Feist             if (ec)
7945f2caaefSJames Feist             {
7955f2caaefSJames Feist                 BMCWEB_LOG_ERROR << "Error patching " << path << ": " << ec;
7965f2caaefSJames Feist                 messages::internalError(response->res);
797b6baeaa4SJames Feist                 return;
7985f2caaefSJames Feist             }
799b6baeaa4SJames Feist             messages::success(response->res);
8005f2caaefSJames Feist             },
8015f2caaefSJames Feist             "xyz.openbmc_project.EntityManager", path, iface, "Delete");
8025f2caaefSJames Feist         return CreatePIDRet::del;
8035f2caaefSJames Feist     }
8045f2caaefSJames Feist 
805711ac7a9SEd Tanous     const dbus::utility::ManagedObjectType::value_type* managedItem = nullptr;
806b6baeaa4SJames Feist     if (!createNewObject)
807b6baeaa4SJames Feist     {
808b6baeaa4SJames Feist         // if we aren't creating a new object, we should be able to find it on
809b6baeaa4SJames Feist         // d-bus
81073df0db0SJames Feist         managedItem = findChassis(managedObj, it.key(), chassis);
81173df0db0SJames Feist         if (managedItem == nullptr)
812b6baeaa4SJames Feist         {
813b6baeaa4SJames Feist             BMCWEB_LOG_ERROR << "Failed to get chassis from config patch";
814ace85d60SEd Tanous             messages::invalidObject(response->res,
815ace85d60SEd Tanous                                     crow::utility::urlFromPieces(
816ace85d60SEd Tanous                                         "redfish", "v1", "Chassis", chassis));
817b6baeaa4SJames Feist             return CreatePIDRet::fail;
818b6baeaa4SJames Feist         }
819b6baeaa4SJames Feist     }
820b6baeaa4SJames Feist 
82126f6976fSEd Tanous     if (!profile.empty() &&
82273df0db0SJames Feist         (type == "PidControllers" || type == "FanControllers" ||
82373df0db0SJames Feist          type == "StepwiseControllers"))
82473df0db0SJames Feist     {
82573df0db0SJames Feist         if (managedItem == nullptr)
82673df0db0SJames Feist         {
827b9d36b47SEd Tanous             output.emplace_back("Profiles", std::vector<std::string>{profile});
82873df0db0SJames Feist         }
82973df0db0SJames Feist         else
83073df0db0SJames Feist         {
83173df0db0SJames Feist             std::string interface;
83273df0db0SJames Feist             if (type == "StepwiseControllers")
83373df0db0SJames Feist             {
83473df0db0SJames Feist                 interface = stepwiseConfigurationIface;
83573df0db0SJames Feist             }
83673df0db0SJames Feist             else
83773df0db0SJames Feist             {
83873df0db0SJames Feist                 interface = pidConfigurationIface;
83973df0db0SJames Feist             }
840711ac7a9SEd Tanous             bool ifaceFound = false;
841711ac7a9SEd Tanous             for (const auto& iface : managedItem->second)
842711ac7a9SEd Tanous             {
843711ac7a9SEd Tanous                 if (iface.first == interface)
844711ac7a9SEd Tanous                 {
845711ac7a9SEd Tanous                     ifaceFound = true;
846711ac7a9SEd Tanous                     for (const auto& prop : iface.second)
847711ac7a9SEd Tanous                     {
848711ac7a9SEd Tanous                         if (prop.first == "Profiles")
849711ac7a9SEd Tanous                         {
850711ac7a9SEd Tanous                             const std::vector<std::string>* curProfiles =
851711ac7a9SEd Tanous                                 std::get_if<std::vector<std::string>>(
852711ac7a9SEd Tanous                                     &(prop.second));
853711ac7a9SEd Tanous                             if (curProfiles == nullptr)
854711ac7a9SEd Tanous                             {
855711ac7a9SEd Tanous                                 BMCWEB_LOG_ERROR
856711ac7a9SEd Tanous                                     << "Illegal profiles in managed object";
857711ac7a9SEd Tanous                                 messages::internalError(response->res);
858711ac7a9SEd Tanous                                 return CreatePIDRet::fail;
859711ac7a9SEd Tanous                             }
860711ac7a9SEd Tanous                             if (std::find(curProfiles->begin(),
861711ac7a9SEd Tanous                                           curProfiles->end(),
862711ac7a9SEd Tanous                                           profile) == curProfiles->end())
863711ac7a9SEd Tanous                             {
864711ac7a9SEd Tanous                                 std::vector<std::string> newProfiles =
865711ac7a9SEd Tanous                                     *curProfiles;
866711ac7a9SEd Tanous                                 newProfiles.push_back(profile);
867b9d36b47SEd Tanous                                 output.emplace_back("Profiles", newProfiles);
868711ac7a9SEd Tanous                             }
869711ac7a9SEd Tanous                         }
870711ac7a9SEd Tanous                     }
871711ac7a9SEd Tanous                 }
872711ac7a9SEd Tanous             }
873711ac7a9SEd Tanous 
874711ac7a9SEd Tanous             if (!ifaceFound)
87573df0db0SJames Feist             {
87673df0db0SJames Feist                 BMCWEB_LOG_ERROR
87773df0db0SJames Feist                     << "Failed to find interface in managed object";
87873df0db0SJames Feist                 messages::internalError(response->res);
87973df0db0SJames Feist                 return CreatePIDRet::fail;
88073df0db0SJames Feist             }
88173df0db0SJames Feist         }
88273df0db0SJames Feist     }
88373df0db0SJames Feist 
88483ff9ab6SJames Feist     if (type == "PidControllers" || type == "FanControllers")
88583ff9ab6SJames Feist     {
88683ff9ab6SJames Feist         if (createNewObject)
88783ff9ab6SJames Feist         {
888b9d36b47SEd Tanous             output.emplace_back("Class",
889b9d36b47SEd Tanous                                 type == "PidControllers" ? "temp" : "fan");
890b9d36b47SEd Tanous             output.emplace_back("Type", "Pid");
89183ff9ab6SJames Feist         }
8925f2caaefSJames Feist 
8935f2caaefSJames Feist         std::optional<std::vector<nlohmann::json>> zones;
8945f2caaefSJames Feist         std::optional<std::vector<std::string>> inputs;
8955f2caaefSJames Feist         std::optional<std::vector<std::string>> outputs;
8965f2caaefSJames Feist         std::map<std::string, std::optional<double>> doubles;
897b943aaefSJames Feist         std::optional<std::string> setpointOffset;
8985f2caaefSJames Feist         if (!redfish::json_util::readJson(
899b6baeaa4SJames Feist                 it.value(), response->res, "Inputs", inputs, "Outputs", outputs,
9005f2caaefSJames Feist                 "Zones", zones, "FFGainCoefficient",
9015f2caaefSJames Feist                 doubles["FFGainCoefficient"], "FFOffCoefficient",
9025f2caaefSJames Feist                 doubles["FFOffCoefficient"], "ICoefficient",
9035f2caaefSJames Feist                 doubles["ICoefficient"], "ILimitMax", doubles["ILimitMax"],
9045f2caaefSJames Feist                 "ILimitMin", doubles["ILimitMin"], "OutLimitMax",
9055f2caaefSJames Feist                 doubles["OutLimitMax"], "OutLimitMin", doubles["OutLimitMin"],
9065f2caaefSJames Feist                 "PCoefficient", doubles["PCoefficient"], "SetPoint",
907b943aaefSJames Feist                 doubles["SetPoint"], "SetPointOffset", setpointOffset,
908b943aaefSJames Feist                 "SlewNeg", doubles["SlewNeg"], "SlewPos", doubles["SlewPos"],
909b943aaefSJames Feist                 "PositiveHysteresis", doubles["PositiveHysteresis"],
910b943aaefSJames Feist                 "NegativeHysteresis", doubles["NegativeHysteresis"]))
91183ff9ab6SJames Feist         {
91271f52d96SEd Tanous             BMCWEB_LOG_ERROR
91371f52d96SEd Tanous                 << "Illegal Property "
91471f52d96SEd Tanous                 << it.value().dump(2, ' ', true,
91571f52d96SEd Tanous                                    nlohmann::json::error_handler_t::replace);
9165f2caaefSJames Feist             return CreatePIDRet::fail;
91783ff9ab6SJames Feist         }
9185f2caaefSJames Feist         if (zones)
9195f2caaefSJames Feist         {
9205f2caaefSJames Feist             std::vector<std::string> zonesStr;
9215f2caaefSJames Feist             if (!getZonesFromJsonReq(response, *zones, zonesStr))
9225f2caaefSJames Feist             {
923a0744d38SGunnar Mills                 BMCWEB_LOG_ERROR << "Illegal Zones";
9245f2caaefSJames Feist                 return CreatePIDRet::fail;
9255f2caaefSJames Feist             }
926b6baeaa4SJames Feist             if (chassis.empty() &&
927e662eae8SEd Tanous                 findChassis(managedObj, zonesStr[0], chassis) == nullptr)
928b6baeaa4SJames Feist             {
929b6baeaa4SJames Feist                 BMCWEB_LOG_ERROR << "Failed to get chassis from config patch";
930ace85d60SEd Tanous                 messages::invalidObject(
931ace85d60SEd Tanous                     response->res, crow::utility::urlFromPieces(
932ace85d60SEd Tanous                                        "redfish", "v1", "Chassis", chassis));
933b6baeaa4SJames Feist                 return CreatePIDRet::fail;
934b6baeaa4SJames Feist             }
935b9d36b47SEd Tanous             output.emplace_back("Zones", std::move(zonesStr));
9365f2caaefSJames Feist         }
9375f2caaefSJames Feist         if (inputs || outputs)
9385f2caaefSJames Feist         {
93902cad96eSEd Tanous             std::array<
94002cad96eSEd Tanous                 std::reference_wrapper<std::optional<std::vector<std::string>>>,
94102cad96eSEd Tanous                 2>
94202cad96eSEd Tanous                 containers = {inputs, outputs};
9435f2caaefSJames Feist             size_t index = 0;
94402cad96eSEd Tanous             for (std::optional<std::vector<std::string>>& container :
94502cad96eSEd Tanous                  containers)
9465f2caaefSJames Feist             {
9475f2caaefSJames Feist                 if (!container)
9485f2caaefSJames Feist                 {
9495f2caaefSJames Feist                     index++;
9505f2caaefSJames Feist                     continue;
95183ff9ab6SJames Feist                 }
9525f2caaefSJames Feist                 for (std::string& value : *container)
95383ff9ab6SJames Feist                 {
954*a170f275SEd Tanous                     std::replace(value.begin(), value.end(), '_', ' ');
95583ff9ab6SJames Feist                 }
9565f2caaefSJames Feist                 std::string key;
9575f2caaefSJames Feist                 if (index == 0)
9585f2caaefSJames Feist                 {
9595f2caaefSJames Feist                     key = "Inputs";
9605f2caaefSJames Feist                 }
9615f2caaefSJames Feist                 else
9625f2caaefSJames Feist                 {
9635f2caaefSJames Feist                     key = "Outputs";
9645f2caaefSJames Feist                 }
965b9d36b47SEd Tanous                 output.emplace_back(key, *container);
9665f2caaefSJames Feist                 index++;
9675f2caaefSJames Feist             }
96883ff9ab6SJames Feist         }
96983ff9ab6SJames Feist 
970b943aaefSJames Feist         if (setpointOffset)
971b943aaefSJames Feist         {
972b943aaefSJames Feist             // translate between redfish and dbus names
973b943aaefSJames Feist             if (*setpointOffset == "UpperThresholdNonCritical")
974b943aaefSJames Feist             {
975b9d36b47SEd Tanous                 output.emplace_back("SetPointOffset", "WarningLow");
976b943aaefSJames Feist             }
977b943aaefSJames Feist             else if (*setpointOffset == "LowerThresholdNonCritical")
978b943aaefSJames Feist             {
979b9d36b47SEd Tanous                 output.emplace_back("SetPointOffset", "WarningHigh");
980b943aaefSJames Feist             }
981b943aaefSJames Feist             else if (*setpointOffset == "LowerThresholdCritical")
982b943aaefSJames Feist             {
983b9d36b47SEd Tanous                 output.emplace_back("SetPointOffset", "CriticalLow");
984b943aaefSJames Feist             }
985b943aaefSJames Feist             else if (*setpointOffset == "UpperThresholdCritical")
986b943aaefSJames Feist             {
987b9d36b47SEd Tanous                 output.emplace_back("SetPointOffset", "CriticalHigh");
988b943aaefSJames Feist             }
989b943aaefSJames Feist             else
990b943aaefSJames Feist             {
991b943aaefSJames Feist                 BMCWEB_LOG_ERROR << "Invalid setpointoffset "
992b943aaefSJames Feist                                  << *setpointOffset;
993ace85d60SEd Tanous                 messages::propertyValueNotInList(response->res, it.key(),
994ace85d60SEd Tanous                                                  "SetPointOffset");
995b943aaefSJames Feist                 return CreatePIDRet::fail;
996b943aaefSJames Feist             }
997b943aaefSJames Feist         }
998b943aaefSJames Feist 
99983ff9ab6SJames Feist         // doubles
10005f2caaefSJames Feist         for (const auto& pairs : doubles)
100183ff9ab6SJames Feist         {
10025f2caaefSJames Feist             if (!pairs.second)
100383ff9ab6SJames Feist             {
10045f2caaefSJames Feist                 continue;
100583ff9ab6SJames Feist             }
10065f2caaefSJames Feist             BMCWEB_LOG_DEBUG << pairs.first << " = " << *pairs.second;
1007b9d36b47SEd Tanous             output.emplace_back(pairs.first, *pairs.second);
10085f2caaefSJames Feist         }
100983ff9ab6SJames Feist     }
101083ff9ab6SJames Feist 
101183ff9ab6SJames Feist     else if (type == "FanZones")
101283ff9ab6SJames Feist     {
1013b9d36b47SEd Tanous         output.emplace_back("Type", "Pid.Zone");
101483ff9ab6SJames Feist 
10155f2caaefSJames Feist         std::optional<nlohmann::json> chassisContainer;
10165f2caaefSJames Feist         std::optional<double> failSafePercent;
1017d3ec07f8SJames Feist         std::optional<double> minThermalOutput;
1018b6baeaa4SJames Feist         if (!redfish::json_util::readJson(it.value(), response->res, "Chassis",
10195f2caaefSJames Feist                                           chassisContainer, "FailSafePercent",
1020d3ec07f8SJames Feist                                           failSafePercent, "MinThermalOutput",
1021d3ec07f8SJames Feist                                           minThermalOutput))
102283ff9ab6SJames Feist         {
102371f52d96SEd Tanous             BMCWEB_LOG_ERROR
102471f52d96SEd Tanous                 << "Illegal Property "
102571f52d96SEd Tanous                 << it.value().dump(2, ' ', true,
102671f52d96SEd Tanous                                    nlohmann::json::error_handler_t::replace);
102783ff9ab6SJames Feist             return CreatePIDRet::fail;
102883ff9ab6SJames Feist         }
10295f2caaefSJames Feist 
10305f2caaefSJames Feist         if (chassisContainer)
103183ff9ab6SJames Feist         {
10325f2caaefSJames Feist 
10335f2caaefSJames Feist             std::string chassisId;
10345f2caaefSJames Feist             if (!redfish::json_util::readJson(*chassisContainer, response->res,
10355f2caaefSJames Feist                                               "@odata.id", chassisId))
10365f2caaefSJames Feist             {
103771f52d96SEd Tanous                 BMCWEB_LOG_ERROR
103871f52d96SEd Tanous                     << "Illegal Property "
103971f52d96SEd Tanous                     << chassisContainer->dump(
104071f52d96SEd Tanous                            2, ' ', true,
104171f52d96SEd Tanous                            nlohmann::json::error_handler_t::replace);
104283ff9ab6SJames Feist                 return CreatePIDRet::fail;
104383ff9ab6SJames Feist             }
104483ff9ab6SJames Feist 
1045717794d5SAppaRao Puli             // /redfish/v1/chassis/chassis_name/
10465f2caaefSJames Feist             if (!dbus::utility::getNthStringFromPath(chassisId, 3, chassis))
104783ff9ab6SJames Feist             {
10485f2caaefSJames Feist                 BMCWEB_LOG_ERROR << "Got invalid path " << chassisId;
1049ace85d60SEd Tanous                 messages::invalidObject(
1050ace85d60SEd Tanous                     response->res, crow::utility::urlFromPieces(
1051ace85d60SEd Tanous                                        "redfish", "v1", "Chassis", chassisId));
105283ff9ab6SJames Feist                 return CreatePIDRet::fail;
105383ff9ab6SJames Feist             }
105483ff9ab6SJames Feist         }
1055d3ec07f8SJames Feist         if (minThermalOutput)
105683ff9ab6SJames Feist         {
1057b9d36b47SEd Tanous             output.emplace_back("MinThermalOutput", *minThermalOutput);
10585f2caaefSJames Feist         }
10595f2caaefSJames Feist         if (failSafePercent)
106083ff9ab6SJames Feist         {
1061b9d36b47SEd Tanous             output.emplace_back("FailSafePercent", *failSafePercent);
10625f2caaefSJames Feist         }
10635f2caaefSJames Feist     }
10645f2caaefSJames Feist     else if (type == "StepwiseControllers")
10655f2caaefSJames Feist     {
1066b9d36b47SEd Tanous         output.emplace_back("Type", "Stepwise");
10675f2caaefSJames Feist 
10685f2caaefSJames Feist         std::optional<std::vector<nlohmann::json>> zones;
10695f2caaefSJames Feist         std::optional<std::vector<nlohmann::json>> steps;
10705f2caaefSJames Feist         std::optional<std::vector<std::string>> inputs;
10715f2caaefSJames Feist         std::optional<double> positiveHysteresis;
10725f2caaefSJames Feist         std::optional<double> negativeHysteresis;
1073c33a90ecSJames Feist         std::optional<std::string> direction; // upper clipping curve vs lower
10745f2caaefSJames Feist         if (!redfish::json_util::readJson(
1075b6baeaa4SJames Feist                 it.value(), response->res, "Zones", zones, "Steps", steps,
1076b6baeaa4SJames Feist                 "Inputs", inputs, "PositiveHysteresis", positiveHysteresis,
1077c33a90ecSJames Feist                 "NegativeHysteresis", negativeHysteresis, "Direction",
1078c33a90ecSJames Feist                 direction))
10795f2caaefSJames Feist         {
108071f52d96SEd Tanous             BMCWEB_LOG_ERROR
108171f52d96SEd Tanous                 << "Illegal Property "
108271f52d96SEd Tanous                 << it.value().dump(2, ' ', true,
108371f52d96SEd Tanous                                    nlohmann::json::error_handler_t::replace);
108483ff9ab6SJames Feist             return CreatePIDRet::fail;
108583ff9ab6SJames Feist         }
10865f2caaefSJames Feist 
10875f2caaefSJames Feist         if (zones)
108883ff9ab6SJames Feist         {
1089b6baeaa4SJames Feist             std::vector<std::string> zonesStrs;
1090b6baeaa4SJames Feist             if (!getZonesFromJsonReq(response, *zones, zonesStrs))
10915f2caaefSJames Feist             {
1092a0744d38SGunnar Mills                 BMCWEB_LOG_ERROR << "Illegal Zones";
109383ff9ab6SJames Feist                 return CreatePIDRet::fail;
109483ff9ab6SJames Feist             }
1095b6baeaa4SJames Feist             if (chassis.empty() &&
1096e662eae8SEd Tanous                 findChassis(managedObj, zonesStrs[0], chassis) == nullptr)
1097b6baeaa4SJames Feist             {
1098b6baeaa4SJames Feist                 BMCWEB_LOG_ERROR << "Failed to get chassis from config patch";
1099ace85d60SEd Tanous                 messages::invalidObject(
1100ace85d60SEd Tanous                     response->res, crow::utility::urlFromPieces(
1101ace85d60SEd Tanous                                        "redfish", "v1", "Chassis", chassis));
1102b6baeaa4SJames Feist                 return CreatePIDRet::fail;
1103b6baeaa4SJames Feist             }
1104b9d36b47SEd Tanous             output.emplace_back("Zones", std::move(zonesStrs));
11055f2caaefSJames Feist         }
11065f2caaefSJames Feist         if (steps)
11075f2caaefSJames Feist         {
11085f2caaefSJames Feist             std::vector<double> readings;
11095f2caaefSJames Feist             std::vector<double> outputs;
11105f2caaefSJames Feist             for (auto& step : *steps)
11115f2caaefSJames Feist             {
1112543f4400SEd Tanous                 double target = 0.0;
1113543f4400SEd Tanous                 double out = 0.0;
11145f2caaefSJames Feist 
11155f2caaefSJames Feist                 if (!redfish::json_util::readJson(step, response->res, "Target",
111623a21a1cSEd Tanous                                                   target, "Output", out))
11175f2caaefSJames Feist                 {
111871f52d96SEd Tanous                     BMCWEB_LOG_ERROR
111971f52d96SEd Tanous                         << "Illegal Property "
112071f52d96SEd Tanous                         << it.value().dump(
112171f52d96SEd Tanous                                2, ' ', true,
112271f52d96SEd Tanous                                nlohmann::json::error_handler_t::replace);
11235f2caaefSJames Feist                     return CreatePIDRet::fail;
11245f2caaefSJames Feist                 }
11255f2caaefSJames Feist                 readings.emplace_back(target);
112623a21a1cSEd Tanous                 outputs.emplace_back(out);
11275f2caaefSJames Feist             }
1128b9d36b47SEd Tanous             output.emplace_back("Reading", std::move(readings));
1129b9d36b47SEd Tanous             output.emplace_back("Output", std::move(outputs));
11305f2caaefSJames Feist         }
11315f2caaefSJames Feist         if (inputs)
11325f2caaefSJames Feist         {
11335f2caaefSJames Feist             for (std::string& value : *inputs)
11345f2caaefSJames Feist             {
1135*a170f275SEd Tanous 
1136*a170f275SEd Tanous                 std::replace(value.begin(), value.end(), '_', ' ');
11375f2caaefSJames Feist             }
1138b9d36b47SEd Tanous             output.emplace_back("Inputs", std::move(*inputs));
11395f2caaefSJames Feist         }
11405f2caaefSJames Feist         if (negativeHysteresis)
11415f2caaefSJames Feist         {
1142b9d36b47SEd Tanous             output.emplace_back("NegativeHysteresis", *negativeHysteresis);
11435f2caaefSJames Feist         }
11445f2caaefSJames Feist         if (positiveHysteresis)
11455f2caaefSJames Feist         {
1146b9d36b47SEd Tanous             output.emplace_back("PositiveHysteresis", *positiveHysteresis);
114783ff9ab6SJames Feist         }
1148c33a90ecSJames Feist         if (direction)
1149c33a90ecSJames Feist         {
1150c33a90ecSJames Feist             constexpr const std::array<const char*, 2> allowedDirections = {
1151c33a90ecSJames Feist                 "Ceiling", "Floor"};
1152c33a90ecSJames Feist             if (std::find(allowedDirections.begin(), allowedDirections.end(),
1153c33a90ecSJames Feist                           *direction) == allowedDirections.end())
1154c33a90ecSJames Feist             {
1155c33a90ecSJames Feist                 messages::propertyValueTypeError(response->res, "Direction",
1156c33a90ecSJames Feist                                                  *direction);
1157c33a90ecSJames Feist                 return CreatePIDRet::fail;
1158c33a90ecSJames Feist             }
1159b9d36b47SEd Tanous             output.emplace_back("Class", *direction);
1160c33a90ecSJames Feist         }
116183ff9ab6SJames Feist     }
116283ff9ab6SJames Feist     else
116383ff9ab6SJames Feist     {
1164a0744d38SGunnar Mills         BMCWEB_LOG_ERROR << "Illegal Type " << type;
116535a62c7cSJason M. Bills         messages::propertyUnknown(response->res, type);
116683ff9ab6SJames Feist         return CreatePIDRet::fail;
116783ff9ab6SJames Feist     }
116883ff9ab6SJames Feist     return CreatePIDRet::patch;
116983ff9ab6SJames Feist }
117073df0db0SJames Feist struct GetPIDValues : std::enable_shared_from_this<GetPIDValues>
117173df0db0SJames Feist {
117283ff9ab6SJames Feist 
11734e23a444SEd Tanous     explicit GetPIDValues(
11744e23a444SEd Tanous         const std::shared_ptr<bmcweb::AsyncResp>& asyncRespIn) :
117523a21a1cSEd Tanous         asyncResp(asyncRespIn)
117673df0db0SJames Feist 
11771214b7e7SGunnar Mills     {}
11789c310685SBorawski.Lukasz 
117973df0db0SJames Feist     void run()
11805b4aa86bSJames Feist     {
118173df0db0SJames Feist         std::shared_ptr<GetPIDValues> self = shared_from_this();
118273df0db0SJames Feist 
118373df0db0SJames Feist         // get all configurations
11845b4aa86bSJames Feist         crow::connections::systemBus->async_method_call(
1185b9d36b47SEd Tanous             [self](
1186b9d36b47SEd Tanous                 const boost::system::error_code ec,
1187b9d36b47SEd Tanous                 const dbus::utility::MapperGetSubTreeResponse& subtreeLocal) {
11885b4aa86bSJames Feist             if (ec)
11895b4aa86bSJames Feist             {
11905b4aa86bSJames Feist                 BMCWEB_LOG_ERROR << ec;
119173df0db0SJames Feist                 messages::internalError(self->asyncResp->res);
119273df0db0SJames Feist                 return;
119373df0db0SJames Feist             }
119423a21a1cSEd Tanous             self->subtree = subtreeLocal;
119573df0db0SJames Feist             },
119673df0db0SJames Feist             "xyz.openbmc_project.ObjectMapper",
119773df0db0SJames Feist             "/xyz/openbmc_project/object_mapper",
119873df0db0SJames Feist             "xyz.openbmc_project.ObjectMapper", "GetSubTree", "/", 0,
119973df0db0SJames Feist             std::array<const char*, 4>{
120073df0db0SJames Feist                 pidConfigurationIface, pidZoneConfigurationIface,
120173df0db0SJames Feist                 objectManagerIface, stepwiseConfigurationIface});
120273df0db0SJames Feist 
120373df0db0SJames Feist         // at the same time get the selected profile
120473df0db0SJames Feist         crow::connections::systemBus->async_method_call(
1205b9d36b47SEd Tanous             [self](
1206b9d36b47SEd Tanous                 const boost::system::error_code ec,
1207b9d36b47SEd Tanous                 const dbus::utility::MapperGetSubTreeResponse& subtreeLocal) {
120823a21a1cSEd Tanous             if (ec || subtreeLocal.empty())
120973df0db0SJames Feist             {
121073df0db0SJames Feist                 return;
121173df0db0SJames Feist             }
121223a21a1cSEd Tanous             if (subtreeLocal[0].second.size() != 1)
121373df0db0SJames Feist             {
121473df0db0SJames Feist                 // invalid mapper response, should never happen
121573df0db0SJames Feist                 BMCWEB_LOG_ERROR << "GetPIDValues: Mapper Error";
121673df0db0SJames Feist                 messages::internalError(self->asyncResp->res);
12175b4aa86bSJames Feist                 return;
12185b4aa86bSJames Feist             }
12195b4aa86bSJames Feist 
122023a21a1cSEd Tanous             const std::string& path = subtreeLocal[0].first;
122123a21a1cSEd Tanous             const std::string& owner = subtreeLocal[0].second[0].first;
122273df0db0SJames Feist             crow::connections::systemBus->async_method_call(
1223168e20c1SEd Tanous                 [path, owner,
1224168e20c1SEd Tanous                  self](const boost::system::error_code ec2,
1225b9d36b47SEd Tanous                        const dbus::utility::DBusPropertiesMap& resp) {
122623a21a1cSEd Tanous                 if (ec2)
122773df0db0SJames Feist                 {
12280fda0f12SGeorge Liu                     BMCWEB_LOG_ERROR
1229002d39b4SEd Tanous                         << "GetPIDValues: Can't get thermalModeIface " << path;
123073df0db0SJames Feist                     messages::internalError(self->asyncResp->res);
123173df0db0SJames Feist                     return;
123273df0db0SJames Feist                 }
1233271584abSEd Tanous                 const std::string* current = nullptr;
1234271584abSEd Tanous                 const std::vector<std::string>* supported = nullptr;
12359eb808c1SEd Tanous                 for (const auto& [key, value] : resp)
123673df0db0SJames Feist                 {
123773df0db0SJames Feist                     if (key == "Current")
123873df0db0SJames Feist                     {
123973df0db0SJames Feist                         current = std::get_if<std::string>(&value);
124073df0db0SJames Feist                         if (current == nullptr)
124173df0db0SJames Feist                         {
124273df0db0SJames Feist                             BMCWEB_LOG_ERROR
12430fda0f12SGeorge Liu                                 << "GetPIDValues: thermal mode iface invalid "
124473df0db0SJames Feist                                 << path;
1245002d39b4SEd Tanous                             messages::internalError(self->asyncResp->res);
124673df0db0SJames Feist                             return;
124773df0db0SJames Feist                         }
124873df0db0SJames Feist                     }
124973df0db0SJames Feist                     if (key == "Supported")
125073df0db0SJames Feist                     {
125173df0db0SJames Feist                         supported =
1252002d39b4SEd Tanous                             std::get_if<std::vector<std::string>>(&value);
125373df0db0SJames Feist                         if (supported == nullptr)
125473df0db0SJames Feist                         {
125573df0db0SJames Feist                             BMCWEB_LOG_ERROR
12560fda0f12SGeorge Liu                                 << "GetPIDValues: thermal mode iface invalid"
125773df0db0SJames Feist                                 << path;
1258002d39b4SEd Tanous                             messages::internalError(self->asyncResp->res);
125973df0db0SJames Feist                             return;
126073df0db0SJames Feist                         }
126173df0db0SJames Feist                     }
126273df0db0SJames Feist                 }
126373df0db0SJames Feist                 if (current == nullptr || supported == nullptr)
126473df0db0SJames Feist                 {
12650fda0f12SGeorge Liu                     BMCWEB_LOG_ERROR
1266002d39b4SEd Tanous                         << "GetPIDValues: thermal mode iface invalid " << path;
126773df0db0SJames Feist                     messages::internalError(self->asyncResp->res);
126873df0db0SJames Feist                     return;
126973df0db0SJames Feist                 }
127073df0db0SJames Feist                 self->currentProfile = *current;
127173df0db0SJames Feist                 self->supportedProfiles = *supported;
127273df0db0SJames Feist                 },
127373df0db0SJames Feist                 owner, path, "org.freedesktop.DBus.Properties", "GetAll",
127473df0db0SJames Feist                 thermalModeIface);
127573df0db0SJames Feist             },
127673df0db0SJames Feist             "xyz.openbmc_project.ObjectMapper",
127773df0db0SJames Feist             "/xyz/openbmc_project/object_mapper",
127873df0db0SJames Feist             "xyz.openbmc_project.ObjectMapper", "GetSubTree", "/", 0,
127973df0db0SJames Feist             std::array<const char*, 1>{thermalModeIface});
128073df0db0SJames Feist     }
128173df0db0SJames Feist 
128273df0db0SJames Feist     ~GetPIDValues()
128373df0db0SJames Feist     {
128473df0db0SJames Feist         if (asyncResp->res.result() != boost::beast::http::status::ok)
128573df0db0SJames Feist         {
128673df0db0SJames Feist             return;
128773df0db0SJames Feist         }
12885b4aa86bSJames Feist         // create map of <connection, path to objMgr>>
128973df0db0SJames Feist         boost::container::flat_map<std::string, std::string> objectMgrPaths;
12906bce33bcSJames Feist         boost::container::flat_set<std::string> calledConnections;
12915b4aa86bSJames Feist         for (const auto& pathGroup : subtree)
12925b4aa86bSJames Feist         {
12935b4aa86bSJames Feist             for (const auto& connectionGroup : pathGroup.second)
12945b4aa86bSJames Feist             {
12956bce33bcSJames Feist                 auto findConnection =
12966bce33bcSJames Feist                     calledConnections.find(connectionGroup.first);
12976bce33bcSJames Feist                 if (findConnection != calledConnections.end())
12986bce33bcSJames Feist                 {
12996bce33bcSJames Feist                     break;
13006bce33bcSJames Feist                 }
130173df0db0SJames Feist                 for (const std::string& interface : connectionGroup.second)
13025b4aa86bSJames Feist                 {
13035b4aa86bSJames Feist                     if (interface == objectManagerIface)
13045b4aa86bSJames Feist                     {
130573df0db0SJames Feist                         objectMgrPaths[connectionGroup.first] = pathGroup.first;
13065b4aa86bSJames Feist                     }
13075b4aa86bSJames Feist                     // this list is alphabetical, so we
13085b4aa86bSJames Feist                     // should have found the objMgr by now
13095b4aa86bSJames Feist                     if (interface == pidConfigurationIface ||
1310b7a08d04SJames Feist                         interface == pidZoneConfigurationIface ||
1311b7a08d04SJames Feist                         interface == stepwiseConfigurationIface)
13125b4aa86bSJames Feist                     {
13135b4aa86bSJames Feist                         auto findObjMgr =
13145b4aa86bSJames Feist                             objectMgrPaths.find(connectionGroup.first);
13155b4aa86bSJames Feist                         if (findObjMgr == objectMgrPaths.end())
13165b4aa86bSJames Feist                         {
13175b4aa86bSJames Feist                             BMCWEB_LOG_DEBUG << connectionGroup.first
13185b4aa86bSJames Feist                                              << "Has no Object Manager";
13195b4aa86bSJames Feist                             continue;
13205b4aa86bSJames Feist                         }
13216bce33bcSJames Feist 
13226bce33bcSJames Feist                         calledConnections.insert(connectionGroup.first);
13236bce33bcSJames Feist 
132473df0db0SJames Feist                         asyncPopulatePid(findObjMgr->first, findObjMgr->second,
132573df0db0SJames Feist                                          currentProfile, supportedProfiles,
132673df0db0SJames Feist                                          asyncResp);
13275b4aa86bSJames Feist                         break;
13285b4aa86bSJames Feist                     }
13295b4aa86bSJames Feist                 }
13305b4aa86bSJames Feist             }
13315b4aa86bSJames Feist         }
133273df0db0SJames Feist     }
133373df0db0SJames Feist 
1334ecd6a3a2SEd Tanous     GetPIDValues(const GetPIDValues&) = delete;
1335ecd6a3a2SEd Tanous     GetPIDValues(GetPIDValues&&) = delete;
1336ecd6a3a2SEd Tanous     GetPIDValues& operator=(const GetPIDValues&) = delete;
1337ecd6a3a2SEd Tanous     GetPIDValues& operator=(GetPIDValues&&) = delete;
1338ecd6a3a2SEd Tanous 
133973df0db0SJames Feist     std::vector<std::string> supportedProfiles;
134073df0db0SJames Feist     std::string currentProfile;
1341b9d36b47SEd Tanous     dbus::utility::MapperGetSubTreeResponse subtree;
13428d1b46d7Szhanghch05     std::shared_ptr<bmcweb::AsyncResp> asyncResp;
134373df0db0SJames Feist };
134473df0db0SJames Feist 
134573df0db0SJames Feist struct SetPIDValues : std::enable_shared_from_this<SetPIDValues>
134673df0db0SJames Feist {
134773df0db0SJames Feist 
13488d1b46d7Szhanghch05     SetPIDValues(const std::shared_ptr<bmcweb::AsyncResp>& asyncRespIn,
134973df0db0SJames Feist                  nlohmann::json& data) :
1350271584abSEd Tanous         asyncResp(asyncRespIn)
135173df0db0SJames Feist     {
135273df0db0SJames Feist 
135373df0db0SJames Feist         std::optional<nlohmann::json> pidControllers;
135473df0db0SJames Feist         std::optional<nlohmann::json> fanControllers;
135573df0db0SJames Feist         std::optional<nlohmann::json> fanZones;
135673df0db0SJames Feist         std::optional<nlohmann::json> stepwiseControllers;
135773df0db0SJames Feist 
135873df0db0SJames Feist         if (!redfish::json_util::readJson(
135973df0db0SJames Feist                 data, asyncResp->res, "PidControllers", pidControllers,
136073df0db0SJames Feist                 "FanControllers", fanControllers, "FanZones", fanZones,
136173df0db0SJames Feist                 "StepwiseControllers", stepwiseControllers, "Profile", profile))
136273df0db0SJames Feist         {
136371f52d96SEd Tanous             BMCWEB_LOG_ERROR
136471f52d96SEd Tanous                 << "Illegal Property "
136571f52d96SEd Tanous                 << data.dump(2, ' ', true,
136671f52d96SEd Tanous                              nlohmann::json::error_handler_t::replace);
136773df0db0SJames Feist             return;
136873df0db0SJames Feist         }
136973df0db0SJames Feist         configuration.emplace_back("PidControllers", std::move(pidControllers));
137073df0db0SJames Feist         configuration.emplace_back("FanControllers", std::move(fanControllers));
137173df0db0SJames Feist         configuration.emplace_back("FanZones", std::move(fanZones));
137273df0db0SJames Feist         configuration.emplace_back("StepwiseControllers",
137373df0db0SJames Feist                                    std::move(stepwiseControllers));
137473df0db0SJames Feist     }
1375ecd6a3a2SEd Tanous 
1376ecd6a3a2SEd Tanous     SetPIDValues(const SetPIDValues&) = delete;
1377ecd6a3a2SEd Tanous     SetPIDValues(SetPIDValues&&) = delete;
1378ecd6a3a2SEd Tanous     SetPIDValues& operator=(const SetPIDValues&) = delete;
1379ecd6a3a2SEd Tanous     SetPIDValues& operator=(SetPIDValues&&) = delete;
1380ecd6a3a2SEd Tanous 
138173df0db0SJames Feist     void run()
138273df0db0SJames Feist     {
138373df0db0SJames Feist         if (asyncResp->res.result() != boost::beast::http::status::ok)
138473df0db0SJames Feist         {
138573df0db0SJames Feist             return;
138673df0db0SJames Feist         }
138773df0db0SJames Feist 
138873df0db0SJames Feist         std::shared_ptr<SetPIDValues> self = shared_from_this();
138973df0db0SJames Feist 
139073df0db0SJames Feist         // todo(james): might make sense to do a mapper call here if this
139173df0db0SJames Feist         // interface gets more traction
139273df0db0SJames Feist         crow::connections::systemBus->async_method_call(
139373df0db0SJames Feist             [self](const boost::system::error_code ec,
1394914e2d5dSEd Tanous                    const dbus::utility::ManagedObjectType& mObj) {
139573df0db0SJames Feist             if (ec)
139673df0db0SJames Feist             {
139773df0db0SJames Feist                 BMCWEB_LOG_ERROR << "Error communicating to Entity Manager";
139873df0db0SJames Feist                 messages::internalError(self->asyncResp->res);
139973df0db0SJames Feist                 return;
140073df0db0SJames Feist             }
1401e69d9de2SJames Feist             const std::array<const char*, 3> configurations = {
1402e69d9de2SJames Feist                 pidConfigurationIface, pidZoneConfigurationIface,
1403e69d9de2SJames Feist                 stepwiseConfigurationIface};
1404e69d9de2SJames Feist 
140514b0b8d5SJames Feist             for (const auto& [path, object] : mObj)
1406e69d9de2SJames Feist             {
140714b0b8d5SJames Feist                 for (const auto& [interface, _] : object)
1408e69d9de2SJames Feist                 {
1409002d39b4SEd Tanous                     if (std::find(configurations.begin(), configurations.end(),
1410e69d9de2SJames Feist                                   interface) != configurations.end())
1411e69d9de2SJames Feist                     {
141214b0b8d5SJames Feist                         self->objectCount++;
1413e69d9de2SJames Feist                         break;
1414e69d9de2SJames Feist                     }
1415e69d9de2SJames Feist                 }
1416e69d9de2SJames Feist             }
1417914e2d5dSEd Tanous             self->managedObj = mObj;
141873df0db0SJames Feist             },
141973df0db0SJames Feist             "xyz.openbmc_project.EntityManager", "/", objectManagerIface,
142073df0db0SJames Feist             "GetManagedObjects");
142173df0db0SJames Feist 
142273df0db0SJames Feist         // at the same time get the profile information
142373df0db0SJames Feist         crow::connections::systemBus->async_method_call(
142473df0db0SJames Feist             [self](const boost::system::error_code ec,
1425b9d36b47SEd Tanous                    const dbus::utility::MapperGetSubTreeResponse& subtree) {
142673df0db0SJames Feist             if (ec || subtree.empty())
142773df0db0SJames Feist             {
142873df0db0SJames Feist                 return;
142973df0db0SJames Feist             }
143073df0db0SJames Feist             if (subtree[0].second.empty())
143173df0db0SJames Feist             {
143273df0db0SJames Feist                 // invalid mapper response, should never happen
143373df0db0SJames Feist                 BMCWEB_LOG_ERROR << "SetPIDValues: Mapper Error";
143473df0db0SJames Feist                 messages::internalError(self->asyncResp->res);
143573df0db0SJames Feist                 return;
143673df0db0SJames Feist             }
143773df0db0SJames Feist 
143873df0db0SJames Feist             const std::string& path = subtree[0].first;
143973df0db0SJames Feist             const std::string& owner = subtree[0].second[0].first;
144073df0db0SJames Feist             crow::connections::systemBus->async_method_call(
1441002d39b4SEd Tanous                 [self, path, owner](const boost::system::error_code ec2,
1442b9d36b47SEd Tanous                                     const dbus::utility::DBusPropertiesMap& r) {
1443cb13a392SEd Tanous                 if (ec2)
144473df0db0SJames Feist                 {
14450fda0f12SGeorge Liu                     BMCWEB_LOG_ERROR
1446002d39b4SEd Tanous                         << "SetPIDValues: Can't get thermalModeIface " << path;
144773df0db0SJames Feist                     messages::internalError(self->asyncResp->res);
144873df0db0SJames Feist                     return;
144973df0db0SJames Feist                 }
1450271584abSEd Tanous                 const std::string* current = nullptr;
1451271584abSEd Tanous                 const std::vector<std::string>* supported = nullptr;
14529eb808c1SEd Tanous                 for (const auto& [key, value] : r)
145373df0db0SJames Feist                 {
145473df0db0SJames Feist                     if (key == "Current")
145573df0db0SJames Feist                     {
145673df0db0SJames Feist                         current = std::get_if<std::string>(&value);
145773df0db0SJames Feist                         if (current == nullptr)
145873df0db0SJames Feist                         {
145973df0db0SJames Feist                             BMCWEB_LOG_ERROR
14600fda0f12SGeorge Liu                                 << "SetPIDValues: thermal mode iface invalid "
146173df0db0SJames Feist                                 << path;
1462002d39b4SEd Tanous                             messages::internalError(self->asyncResp->res);
146373df0db0SJames Feist                             return;
146473df0db0SJames Feist                         }
146573df0db0SJames Feist                     }
146673df0db0SJames Feist                     if (key == "Supported")
146773df0db0SJames Feist                     {
146873df0db0SJames Feist                         supported =
1469002d39b4SEd Tanous                             std::get_if<std::vector<std::string>>(&value);
147073df0db0SJames Feist                         if (supported == nullptr)
147173df0db0SJames Feist                         {
147273df0db0SJames Feist                             BMCWEB_LOG_ERROR
14730fda0f12SGeorge Liu                                 << "SetPIDValues: thermal mode iface invalid"
147473df0db0SJames Feist                                 << path;
1475002d39b4SEd Tanous                             messages::internalError(self->asyncResp->res);
147673df0db0SJames Feist                             return;
147773df0db0SJames Feist                         }
147873df0db0SJames Feist                     }
147973df0db0SJames Feist                 }
148073df0db0SJames Feist                 if (current == nullptr || supported == nullptr)
148173df0db0SJames Feist                 {
14820fda0f12SGeorge Liu                     BMCWEB_LOG_ERROR
1483002d39b4SEd Tanous                         << "SetPIDValues: thermal mode iface invalid " << path;
148473df0db0SJames Feist                     messages::internalError(self->asyncResp->res);
148573df0db0SJames Feist                     return;
148673df0db0SJames Feist                 }
148773df0db0SJames Feist                 self->currentProfile = *current;
148873df0db0SJames Feist                 self->supportedProfiles = *supported;
148973df0db0SJames Feist                 self->profileConnection = owner;
149073df0db0SJames Feist                 self->profilePath = path;
149173df0db0SJames Feist                 },
149273df0db0SJames Feist                 owner, path, "org.freedesktop.DBus.Properties", "GetAll",
149373df0db0SJames Feist                 thermalModeIface);
14945b4aa86bSJames Feist             },
14955b4aa86bSJames Feist             "xyz.openbmc_project.ObjectMapper",
14965b4aa86bSJames Feist             "/xyz/openbmc_project/object_mapper",
14975b4aa86bSJames Feist             "xyz.openbmc_project.ObjectMapper", "GetSubTree", "/", 0,
149873df0db0SJames Feist             std::array<const char*, 1>{thermalModeIface});
149973df0db0SJames Feist     }
150024b2fe81SEd Tanous     void pidSetDone()
150173df0db0SJames Feist     {
150273df0db0SJames Feist         if (asyncResp->res.result() != boost::beast::http::status::ok)
150373df0db0SJames Feist         {
150473df0db0SJames Feist             return;
15055b4aa86bSJames Feist         }
15068d1b46d7Szhanghch05         std::shared_ptr<bmcweb::AsyncResp> response = asyncResp;
150773df0db0SJames Feist         if (profile)
150873df0db0SJames Feist         {
150973df0db0SJames Feist             if (std::find(supportedProfiles.begin(), supportedProfiles.end(),
151073df0db0SJames Feist                           *profile) == supportedProfiles.end())
151173df0db0SJames Feist             {
151273df0db0SJames Feist                 messages::actionParameterUnknown(response->res, "Profile",
151373df0db0SJames Feist                                                  *profile);
151473df0db0SJames Feist                 return;
151573df0db0SJames Feist             }
151673df0db0SJames Feist             currentProfile = *profile;
151773df0db0SJames Feist             crow::connections::systemBus->async_method_call(
151873df0db0SJames Feist                 [response](const boost::system::error_code ec) {
151973df0db0SJames Feist                 if (ec)
152073df0db0SJames Feist                 {
152173df0db0SJames Feist                     BMCWEB_LOG_ERROR << "Error patching profile" << ec;
152273df0db0SJames Feist                     messages::internalError(response->res);
152373df0db0SJames Feist                 }
152473df0db0SJames Feist                 },
152573df0db0SJames Feist                 profileConnection, profilePath,
152673df0db0SJames Feist                 "org.freedesktop.DBus.Properties", "Set", thermalModeIface,
1527168e20c1SEd Tanous                 "Current", dbus::utility::DbusVariantType(*profile));
152873df0db0SJames Feist         }
152973df0db0SJames Feist 
153073df0db0SJames Feist         for (auto& containerPair : configuration)
153173df0db0SJames Feist         {
153273df0db0SJames Feist             auto& container = containerPair.second;
153373df0db0SJames Feist             if (!container)
153473df0db0SJames Feist             {
153573df0db0SJames Feist                 continue;
153673df0db0SJames Feist             }
15376ee7f774SJames Feist             BMCWEB_LOG_DEBUG << *container;
15386ee7f774SJames Feist 
153902cad96eSEd Tanous             const std::string& type = containerPair.first;
154073df0db0SJames Feist 
154173df0db0SJames Feist             for (nlohmann::json::iterator it = container->begin();
154217a897dfSManojkiran Eda                  it != container->end(); ++it)
154373df0db0SJames Feist             {
154473df0db0SJames Feist                 const auto& name = it.key();
15456ee7f774SJames Feist                 BMCWEB_LOG_DEBUG << "looking for " << name;
15466ee7f774SJames Feist 
154773df0db0SJames Feist                 auto pathItr =
154873df0db0SJames Feist                     std::find_if(managedObj.begin(), managedObj.end(),
154973df0db0SJames Feist                                  [&name](const auto& obj) {
1550002d39b4SEd Tanous                     return boost::algorithm::ends_with(obj.first.str,
1551002d39b4SEd Tanous                                                        "/" + name);
155273df0db0SJames Feist                     });
1553b9d36b47SEd Tanous                 dbus::utility::DBusPropertiesMap output;
155473df0db0SJames Feist 
155573df0db0SJames Feist                 output.reserve(16); // The pid interface length
155673df0db0SJames Feist 
155773df0db0SJames Feist                 // determines if we're patching entity-manager or
155873df0db0SJames Feist                 // creating a new object
155973df0db0SJames Feist                 bool createNewObject = (pathItr == managedObj.end());
15606ee7f774SJames Feist                 BMCWEB_LOG_DEBUG << "Found = " << !createNewObject;
15616ee7f774SJames Feist 
156273df0db0SJames Feist                 std::string iface;
1563711ac7a9SEd Tanous                 /*
156473df0db0SJames Feist                 if (type == "PidControllers" || type == "FanControllers")
156573df0db0SJames Feist                 {
156673df0db0SJames Feist                     iface = pidConfigurationIface;
156773df0db0SJames Feist                     if (!createNewObject &&
156873df0db0SJames Feist                         pathItr->second.find(pidConfigurationIface) ==
156973df0db0SJames Feist                             pathItr->second.end())
157073df0db0SJames Feist                     {
157173df0db0SJames Feist                         createNewObject = true;
157273df0db0SJames Feist                     }
157373df0db0SJames Feist                 }
157473df0db0SJames Feist                 else if (type == "FanZones")
157573df0db0SJames Feist                 {
157673df0db0SJames Feist                     iface = pidZoneConfigurationIface;
157773df0db0SJames Feist                     if (!createNewObject &&
157873df0db0SJames Feist                         pathItr->second.find(pidZoneConfigurationIface) ==
157973df0db0SJames Feist                             pathItr->second.end())
158073df0db0SJames Feist                     {
158173df0db0SJames Feist 
158273df0db0SJames Feist                         createNewObject = true;
158373df0db0SJames Feist                     }
158473df0db0SJames Feist                 }
158573df0db0SJames Feist                 else if (type == "StepwiseControllers")
158673df0db0SJames Feist                 {
158773df0db0SJames Feist                     iface = stepwiseConfigurationIface;
158873df0db0SJames Feist                     if (!createNewObject &&
158973df0db0SJames Feist                         pathItr->second.find(stepwiseConfigurationIface) ==
159073df0db0SJames Feist                             pathItr->second.end())
159173df0db0SJames Feist                     {
159273df0db0SJames Feist                         createNewObject = true;
159373df0db0SJames Feist                     }
1594711ac7a9SEd Tanous                 }*/
15956ee7f774SJames Feist 
15966ee7f774SJames Feist                 if (createNewObject && it.value() == nullptr)
15976ee7f774SJames Feist                 {
15984e0453b1SGunnar Mills                     // can't delete a non-existent object
15991668ce6dSEd Tanous                     messages::propertyValueNotInList(response->res,
16001668ce6dSEd Tanous                                                      it.value().dump(), name);
16016ee7f774SJames Feist                     continue;
16026ee7f774SJames Feist                 }
16036ee7f774SJames Feist 
16046ee7f774SJames Feist                 std::string path;
16056ee7f774SJames Feist                 if (pathItr != managedObj.end())
16066ee7f774SJames Feist                 {
16076ee7f774SJames Feist                     path = pathItr->first.str;
16086ee7f774SJames Feist                 }
16096ee7f774SJames Feist 
161073df0db0SJames Feist                 BMCWEB_LOG_DEBUG << "Create new = " << createNewObject << "\n";
1611e69d9de2SJames Feist 
1612e69d9de2SJames Feist                 // arbitrary limit to avoid attacks
1613e69d9de2SJames Feist                 constexpr const size_t controllerLimit = 500;
161414b0b8d5SJames Feist                 if (createNewObject && objectCount >= controllerLimit)
1615e69d9de2SJames Feist                 {
1616e69d9de2SJames Feist                     messages::resourceExhaustion(response->res, type);
1617e69d9de2SJames Feist                     continue;
1618e69d9de2SJames Feist                 }
1619*a170f275SEd Tanous                 std::string escaped = name;
1620*a170f275SEd Tanous                 std::replace(escaped.begin(), escaped.end(), '_', ' ');
1621*a170f275SEd Tanous                 output.emplace_back("Name", escaped);
162273df0db0SJames Feist 
162373df0db0SJames Feist                 std::string chassis;
162473df0db0SJames Feist                 CreatePIDRet ret = createPidInterface(
16256ee7f774SJames Feist                     response, type, it, path, managedObj, createNewObject,
16266ee7f774SJames Feist                     output, chassis, currentProfile);
162773df0db0SJames Feist                 if (ret == CreatePIDRet::fail)
162873df0db0SJames Feist                 {
162973df0db0SJames Feist                     return;
163073df0db0SJames Feist                 }
16313174e4dfSEd Tanous                 if (ret == CreatePIDRet::del)
163273df0db0SJames Feist                 {
163373df0db0SJames Feist                     continue;
163473df0db0SJames Feist                 }
163573df0db0SJames Feist 
163673df0db0SJames Feist                 if (!createNewObject)
163773df0db0SJames Feist                 {
163873df0db0SJames Feist                     for (const auto& property : output)
163973df0db0SJames Feist                     {
164073df0db0SJames Feist                         crow::connections::systemBus->async_method_call(
164173df0db0SJames Feist                             [response,
164273df0db0SJames Feist                              propertyName{std::string(property.first)}](
164373df0db0SJames Feist                                 const boost::system::error_code ec) {
164473df0db0SJames Feist                             if (ec)
164573df0db0SJames Feist                             {
164673df0db0SJames Feist                                 BMCWEB_LOG_ERROR << "Error patching "
1647002d39b4SEd Tanous                                                  << propertyName << ": " << ec;
164873df0db0SJames Feist                                 messages::internalError(response->res);
164973df0db0SJames Feist                                 return;
165073df0db0SJames Feist                             }
165173df0db0SJames Feist                             messages::success(response->res);
165273df0db0SJames Feist                             },
16536ee7f774SJames Feist                             "xyz.openbmc_project.EntityManager", path,
165473df0db0SJames Feist                             "org.freedesktop.DBus.Properties", "Set", iface,
165573df0db0SJames Feist                             property.first, property.second);
165673df0db0SJames Feist                     }
165773df0db0SJames Feist                 }
165873df0db0SJames Feist                 else
165973df0db0SJames Feist                 {
166073df0db0SJames Feist                     if (chassis.empty())
166173df0db0SJames Feist                     {
166273df0db0SJames Feist                         BMCWEB_LOG_ERROR << "Failed to get chassis from config";
1663ace85d60SEd Tanous                         messages::internalError(response->res);
166473df0db0SJames Feist                         return;
166573df0db0SJames Feist                     }
166673df0db0SJames Feist 
166773df0db0SJames Feist                     bool foundChassis = false;
166873df0db0SJames Feist                     for (const auto& obj : managedObj)
166973df0db0SJames Feist                     {
167073df0db0SJames Feist                         if (boost::algorithm::ends_with(obj.first.str, chassis))
167173df0db0SJames Feist                         {
167273df0db0SJames Feist                             chassis = obj.first.str;
167373df0db0SJames Feist                             foundChassis = true;
167473df0db0SJames Feist                             break;
167573df0db0SJames Feist                         }
167673df0db0SJames Feist                     }
167773df0db0SJames Feist                     if (!foundChassis)
167873df0db0SJames Feist                     {
167973df0db0SJames Feist                         BMCWEB_LOG_ERROR << "Failed to find chassis on dbus";
168073df0db0SJames Feist                         messages::resourceMissingAtURI(
1681ace85d60SEd Tanous                             response->res,
1682ace85d60SEd Tanous                             crow::utility::urlFromPieces("redfish", "v1",
1683ace85d60SEd Tanous                                                          "Chassis", chassis));
168473df0db0SJames Feist                         return;
168573df0db0SJames Feist                     }
168673df0db0SJames Feist 
168773df0db0SJames Feist                     crow::connections::systemBus->async_method_call(
168873df0db0SJames Feist                         [response](const boost::system::error_code ec) {
168973df0db0SJames Feist                         if (ec)
169073df0db0SJames Feist                         {
169173df0db0SJames Feist                             BMCWEB_LOG_ERROR << "Error Adding Pid Object "
169273df0db0SJames Feist                                              << ec;
169373df0db0SJames Feist                             messages::internalError(response->res);
169473df0db0SJames Feist                             return;
169573df0db0SJames Feist                         }
169673df0db0SJames Feist                         messages::success(response->res);
169773df0db0SJames Feist                         },
169873df0db0SJames Feist                         "xyz.openbmc_project.EntityManager", chassis,
169973df0db0SJames Feist                         "xyz.openbmc_project.AddObject", "AddObject", output);
170073df0db0SJames Feist                 }
170173df0db0SJames Feist             }
170273df0db0SJames Feist         }
170373df0db0SJames Feist     }
170424b2fe81SEd Tanous 
170524b2fe81SEd Tanous     ~SetPIDValues()
170624b2fe81SEd Tanous     {
170724b2fe81SEd Tanous         try
170824b2fe81SEd Tanous         {
170924b2fe81SEd Tanous             pidSetDone();
171024b2fe81SEd Tanous         }
171124b2fe81SEd Tanous         catch (...)
171224b2fe81SEd Tanous         {
171324b2fe81SEd Tanous             BMCWEB_LOG_CRITICAL << "pidSetDone threw exception";
171424b2fe81SEd Tanous         }
171524b2fe81SEd Tanous     }
171624b2fe81SEd Tanous 
17178d1b46d7Szhanghch05     std::shared_ptr<bmcweb::AsyncResp> asyncResp;
171873df0db0SJames Feist     std::vector<std::pair<std::string, std::optional<nlohmann::json>>>
171973df0db0SJames Feist         configuration;
172073df0db0SJames Feist     std::optional<std::string> profile;
172173df0db0SJames Feist     dbus::utility::ManagedObjectType managedObj;
172273df0db0SJames Feist     std::vector<std::string> supportedProfiles;
172373df0db0SJames Feist     std::string currentProfile;
172473df0db0SJames Feist     std::string profileConnection;
172573df0db0SJames Feist     std::string profilePath;
172614b0b8d5SJames Feist     size_t objectCount = 0;
172773df0db0SJames Feist };
172873df0db0SJames Feist 
1729071d8fdfSSunnySrivastava1984 /**
1730071d8fdfSSunnySrivastava1984  * @brief Retrieves BMC manager location data over DBus
1731071d8fdfSSunnySrivastava1984  *
1732071d8fdfSSunnySrivastava1984  * @param[in] aResp Shared pointer for completing asynchronous calls
1733071d8fdfSSunnySrivastava1984  * @param[in] connectionName - service name
1734071d8fdfSSunnySrivastava1984  * @param[in] path - object path
1735071d8fdfSSunnySrivastava1984  * @return none
1736071d8fdfSSunnySrivastava1984  */
17378d1b46d7Szhanghch05 inline void getLocation(const std::shared_ptr<bmcweb::AsyncResp>& aResp,
1738071d8fdfSSunnySrivastava1984                         const std::string& connectionName,
1739071d8fdfSSunnySrivastava1984                         const std::string& path)
1740071d8fdfSSunnySrivastava1984 {
1741071d8fdfSSunnySrivastava1984     BMCWEB_LOG_DEBUG << "Get BMC manager Location data.";
1742071d8fdfSSunnySrivastava1984 
17431e1e598dSJonathan Doman     sdbusplus::asio::getProperty<std::string>(
17441e1e598dSJonathan Doman         *crow::connections::systemBus, connectionName, path,
17451e1e598dSJonathan Doman         "xyz.openbmc_project.Inventory.Decorator.LocationCode", "LocationCode",
1746071d8fdfSSunnySrivastava1984         [aResp](const boost::system::error_code ec,
17471e1e598dSJonathan Doman                 const std::string& property) {
1748071d8fdfSSunnySrivastava1984         if (ec)
1749071d8fdfSSunnySrivastava1984         {
1750071d8fdfSSunnySrivastava1984             BMCWEB_LOG_DEBUG << "DBUS response error for "
1751071d8fdfSSunnySrivastava1984                                 "Location";
1752071d8fdfSSunnySrivastava1984             messages::internalError(aResp->res);
1753071d8fdfSSunnySrivastava1984             return;
1754071d8fdfSSunnySrivastava1984         }
1755071d8fdfSSunnySrivastava1984 
1756071d8fdfSSunnySrivastava1984         aResp->res.jsonValue["Location"]["PartLocation"]["ServiceLabel"] =
17571e1e598dSJonathan Doman             property;
17581e1e598dSJonathan Doman         });
1759071d8fdfSSunnySrivastava1984 }
17607e860f15SJohn Edward Broadbent // avoid name collision systems.hpp
17617e860f15SJohn Edward Broadbent inline void
17627e860f15SJohn Edward Broadbent     managerGetLastResetTime(const std::shared_ptr<bmcweb::AsyncResp>& aResp)
17634bf2b033SGunnar Mills {
17644bf2b033SGunnar Mills     BMCWEB_LOG_DEBUG << "Getting Manager Last Reset Time";
17654bf2b033SGunnar Mills 
17661e1e598dSJonathan Doman     sdbusplus::asio::getProperty<uint64_t>(
17671e1e598dSJonathan Doman         *crow::connections::systemBus, "xyz.openbmc_project.State.BMC",
17681e1e598dSJonathan Doman         "/xyz/openbmc_project/state/bmc0", "xyz.openbmc_project.State.BMC",
17691e1e598dSJonathan Doman         "LastRebootTime",
17704bf2b033SGunnar Mills         [aResp](const boost::system::error_code ec,
17711e1e598dSJonathan Doman                 const uint64_t lastResetTime) {
17724bf2b033SGunnar Mills         if (ec)
17734bf2b033SGunnar Mills         {
17744bf2b033SGunnar Mills             BMCWEB_LOG_DEBUG << "D-BUS response error " << ec;
17754bf2b033SGunnar Mills             return;
17764bf2b033SGunnar Mills         }
17774bf2b033SGunnar Mills 
17784bf2b033SGunnar Mills         // LastRebootTime is epoch time, in milliseconds
17794bf2b033SGunnar Mills         // https://github.com/openbmc/phosphor-dbus-interfaces/blob/7f9a128eb9296e926422ddc312c148b625890bb6/xyz/openbmc_project/State/BMC.interface.yaml#L19
17801e1e598dSJonathan Doman         uint64_t lastResetTimeStamp = lastResetTime / 1000;
17814bf2b033SGunnar Mills 
17824bf2b033SGunnar Mills         // Convert to ISO 8601 standard
17834bf2b033SGunnar Mills         aResp->res.jsonValue["LastResetTime"] =
17841d8782e7SNan Zhou             crow::utility::getDateTimeUint(lastResetTimeStamp);
17851e1e598dSJonathan Doman         });
17864bf2b033SGunnar Mills }
17874bf2b033SGunnar Mills 
17884bfefa74SGunnar Mills /**
17894bfefa74SGunnar Mills  * @brief Set the running firmware image
17904bfefa74SGunnar Mills  *
17914bfefa74SGunnar Mills  * @param[i,o] aResp - Async response object
17924bfefa74SGunnar Mills  * @param[i] runningFirmwareTarget - Image to make the running image
17934bfefa74SGunnar Mills  *
17944bfefa74SGunnar Mills  * @return void
17954bfefa74SGunnar Mills  */
17967e860f15SJohn Edward Broadbent inline void
17977e860f15SJohn Edward Broadbent     setActiveFirmwareImage(const std::shared_ptr<bmcweb::AsyncResp>& aResp,
1798f23b7296SEd Tanous                            const std::string& runningFirmwareTarget)
17994bfefa74SGunnar Mills {
18004bfefa74SGunnar Mills     // Get the Id from /redfish/v1/UpdateService/FirmwareInventory/<Id>
1801f23b7296SEd Tanous     std::string::size_type idPos = runningFirmwareTarget.rfind('/');
18024bfefa74SGunnar Mills     if (idPos == std::string::npos)
18034bfefa74SGunnar Mills     {
18044bfefa74SGunnar Mills         messages::propertyValueNotInList(aResp->res, runningFirmwareTarget,
18054bfefa74SGunnar Mills                                          "@odata.id");
18064bfefa74SGunnar Mills         BMCWEB_LOG_DEBUG << "Can't parse firmware ID!";
18074bfefa74SGunnar Mills         return;
18084bfefa74SGunnar Mills     }
18094bfefa74SGunnar Mills     idPos++;
18104bfefa74SGunnar Mills     if (idPos >= runningFirmwareTarget.size())
18114bfefa74SGunnar Mills     {
18124bfefa74SGunnar Mills         messages::propertyValueNotInList(aResp->res, runningFirmwareTarget,
18134bfefa74SGunnar Mills                                          "@odata.id");
18144bfefa74SGunnar Mills         BMCWEB_LOG_DEBUG << "Invalid firmware ID.";
18154bfefa74SGunnar Mills         return;
18164bfefa74SGunnar Mills     }
18174bfefa74SGunnar Mills     std::string firmwareId = runningFirmwareTarget.substr(idPos);
18184bfefa74SGunnar Mills 
18194bfefa74SGunnar Mills     // Make sure the image is valid before setting priority
18204bfefa74SGunnar Mills     crow::connections::systemBus->async_method_call(
1821711ac7a9SEd Tanous         [aResp, firmwareId,
1822711ac7a9SEd Tanous          runningFirmwareTarget](const boost::system::error_code ec,
1823711ac7a9SEd Tanous                                 dbus::utility::ManagedObjectType& subtree) {
18244bfefa74SGunnar Mills         if (ec)
18254bfefa74SGunnar Mills         {
18264bfefa74SGunnar Mills             BMCWEB_LOG_DEBUG << "D-Bus response error getting objects.";
18274bfefa74SGunnar Mills             messages::internalError(aResp->res);
18284bfefa74SGunnar Mills             return;
18294bfefa74SGunnar Mills         }
18304bfefa74SGunnar Mills 
183126f6976fSEd Tanous         if (subtree.empty())
18324bfefa74SGunnar Mills         {
18334bfefa74SGunnar Mills             BMCWEB_LOG_DEBUG << "Can't find image!";
18344bfefa74SGunnar Mills             messages::internalError(aResp->res);
18354bfefa74SGunnar Mills             return;
18364bfefa74SGunnar Mills         }
18374bfefa74SGunnar Mills 
18384bfefa74SGunnar Mills         bool foundImage = false;
183902cad96eSEd Tanous         for (const auto& object : subtree)
18404bfefa74SGunnar Mills         {
18414bfefa74SGunnar Mills             const std::string& path =
18424bfefa74SGunnar Mills                 static_cast<const std::string&>(object.first);
1843f23b7296SEd Tanous             std::size_t idPos2 = path.rfind('/');
18444bfefa74SGunnar Mills 
18454bfefa74SGunnar Mills             if (idPos2 == std::string::npos)
18464bfefa74SGunnar Mills             {
18474bfefa74SGunnar Mills                 continue;
18484bfefa74SGunnar Mills             }
18494bfefa74SGunnar Mills 
18504bfefa74SGunnar Mills             idPos2++;
18514bfefa74SGunnar Mills             if (idPos2 >= path.size())
18524bfefa74SGunnar Mills             {
18534bfefa74SGunnar Mills                 continue;
18544bfefa74SGunnar Mills             }
18554bfefa74SGunnar Mills 
18564bfefa74SGunnar Mills             if (path.substr(idPos2) == firmwareId)
18574bfefa74SGunnar Mills             {
18584bfefa74SGunnar Mills                 foundImage = true;
18594bfefa74SGunnar Mills                 break;
18604bfefa74SGunnar Mills             }
18614bfefa74SGunnar Mills         }
18624bfefa74SGunnar Mills 
18634bfefa74SGunnar Mills         if (!foundImage)
18644bfefa74SGunnar Mills         {
1865002d39b4SEd Tanous             messages::propertyValueNotInList(aResp->res, runningFirmwareTarget,
1866002d39b4SEd Tanous                                              "@odata.id");
18674bfefa74SGunnar Mills             BMCWEB_LOG_DEBUG << "Invalid firmware ID.";
18684bfefa74SGunnar Mills             return;
18694bfefa74SGunnar Mills         }
18704bfefa74SGunnar Mills 
18718cc8edecSEd Tanous         BMCWEB_LOG_DEBUG << "Setting firmware version " << firmwareId
18728cc8edecSEd Tanous                          << " to priority 0.";
18734bfefa74SGunnar Mills 
18744bfefa74SGunnar Mills         // Only support Immediate
18754bfefa74SGunnar Mills         // An addition could be a Redfish Setting like
18764bfefa74SGunnar Mills         // ActiveSoftwareImageApplyTime and support OnReset
18774bfefa74SGunnar Mills         crow::connections::systemBus->async_method_call(
18788a592810SEd Tanous             [aResp](const boost::system::error_code ec2) {
18798a592810SEd Tanous             if (ec2)
18804bfefa74SGunnar Mills             {
18814bfefa74SGunnar Mills                 BMCWEB_LOG_DEBUG << "D-Bus response error setting.";
18824bfefa74SGunnar Mills                 messages::internalError(aResp->res);
18834bfefa74SGunnar Mills                 return;
18844bfefa74SGunnar Mills             }
18854bfefa74SGunnar Mills             doBMCGracefulRestart(aResp);
18864bfefa74SGunnar Mills             },
18874bfefa74SGunnar Mills 
18884bfefa74SGunnar Mills             "xyz.openbmc_project.Software.BMC.Updater",
18894bfefa74SGunnar Mills             "/xyz/openbmc_project/software/" + firmwareId,
18904bfefa74SGunnar Mills             "org.freedesktop.DBus.Properties", "Set",
18917e860f15SJohn Edward Broadbent             "xyz.openbmc_project.Software.RedundancyPriority", "Priority",
1892168e20c1SEd Tanous             dbus::utility::DbusVariantType(static_cast<uint8_t>(0)));
18934bfefa74SGunnar Mills         },
18944bfefa74SGunnar Mills         "xyz.openbmc_project.Software.BMC.Updater",
18957e860f15SJohn Edward Broadbent         "/xyz/openbmc_project/software", "org.freedesktop.DBus.ObjectManager",
18967e860f15SJohn Edward Broadbent         "GetManagedObjects");
18974bfefa74SGunnar Mills }
18984bfefa74SGunnar Mills 
18997e860f15SJohn Edward Broadbent inline void setDateTime(std::shared_ptr<bmcweb::AsyncResp> aResp,
19007e860f15SJohn Edward Broadbent                         std::string datetime)
1901af5d6058SSantosh Puranik {
1902af5d6058SSantosh Puranik     BMCWEB_LOG_DEBUG << "Set date time: " << datetime;
1903af5d6058SSantosh Puranik 
1904af5d6058SSantosh Puranik     std::stringstream stream(datetime);
1905af5d6058SSantosh Puranik     // Convert from ISO 8601 to boost local_time
1906af5d6058SSantosh Puranik     // (BMC only has time in UTC)
1907af5d6058SSantosh Puranik     boost::posix_time::ptime posixTime;
1908af5d6058SSantosh Puranik     boost::posix_time::ptime epoch(boost::gregorian::date(1970, 1, 1));
1909af5d6058SSantosh Puranik     // Facet gets deleted with the stringsteam
1910af5d6058SSantosh Puranik     auto ifc = std::make_unique<boost::local_time::local_time_input_facet>(
1911af5d6058SSantosh Puranik         "%Y-%m-%d %H:%M:%S%F %ZP");
1912af5d6058SSantosh Puranik     stream.imbue(std::locale(stream.getloc(), ifc.release()));
1913af5d6058SSantosh Puranik 
19147e860f15SJohn Edward Broadbent     boost::local_time::local_date_time ldt(boost::local_time::not_a_date_time);
1915af5d6058SSantosh Puranik 
1916af5d6058SSantosh Puranik     if (stream >> ldt)
1917af5d6058SSantosh Puranik     {
1918af5d6058SSantosh Puranik         posixTime = ldt.utc_time();
1919af5d6058SSantosh Puranik         boost::posix_time::time_duration dur = posixTime - epoch;
19207e860f15SJohn Edward Broadbent         uint64_t durMicroSecs = static_cast<uint64_t>(dur.total_microseconds());
1921af5d6058SSantosh Puranik         crow::connections::systemBus->async_method_call(
1922af5d6058SSantosh Puranik             [aResp{std::move(aResp)}, datetime{std::move(datetime)}](
1923af5d6058SSantosh Puranik                 const boost::system::error_code ec) {
1924af5d6058SSantosh Puranik             if (ec)
1925af5d6058SSantosh Puranik             {
1926af5d6058SSantosh Puranik                 BMCWEB_LOG_DEBUG << "Failed to set elapsed time. "
1927af5d6058SSantosh Puranik                                     "DBUS response error "
1928af5d6058SSantosh Puranik                                  << ec;
1929af5d6058SSantosh Puranik                 messages::internalError(aResp->res);
1930af5d6058SSantosh Puranik                 return;
1931af5d6058SSantosh Puranik             }
1932af5d6058SSantosh Puranik             aResp->res.jsonValue["DateTime"] = datetime;
1933af5d6058SSantosh Puranik             },
19347e860f15SJohn Edward Broadbent             "xyz.openbmc_project.Time.Manager", "/xyz/openbmc_project/time/bmc",
1935af5d6058SSantosh Puranik             "org.freedesktop.DBus.Properties", "Set",
1936af5d6058SSantosh Puranik             "xyz.openbmc_project.Time.EpochTime", "Elapsed",
1937168e20c1SEd Tanous             dbus::utility::DbusVariantType(durMicroSecs));
1938af5d6058SSantosh Puranik     }
1939af5d6058SSantosh Puranik     else
1940af5d6058SSantosh Puranik     {
19417e860f15SJohn Edward Broadbent         messages::propertyValueFormatError(aResp->res, datetime, "DateTime");
1942af5d6058SSantosh Puranik         return;
1943af5d6058SSantosh Puranik     }
194483ff9ab6SJames Feist }
19459c310685SBorawski.Lukasz 
19467e860f15SJohn Edward Broadbent inline void requestRoutesManager(App& app)
19477e860f15SJohn Edward Broadbent {
19487e860f15SJohn Edward Broadbent     std::string uuid = persistent_data::getConfig().systemUuid;
19499c310685SBorawski.Lukasz 
19507e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/")
1951ed398213SEd Tanous         .privileges(redfish::privileges::getManager)
1952002d39b4SEd Tanous         .methods(boost::beast::http::verb::get)(
1953002d39b4SEd Tanous             [&app, uuid](const crow::Request& req,
195445ca1b86SEd Tanous                          const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
19553ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
195645ca1b86SEd Tanous         {
195745ca1b86SEd Tanous             return;
195845ca1b86SEd Tanous         }
19597e860f15SJohn Edward Broadbent         asyncResp->res.jsonValue["@odata.id"] = "/redfish/v1/Managers/bmc";
1960a51fc2d2SSui Chen         asyncResp->res.jsonValue["@odata.type"] = "#Manager.v1_14_0.Manager";
19617e860f15SJohn Edward Broadbent         asyncResp->res.jsonValue["Id"] = "bmc";
19627e860f15SJohn Edward Broadbent         asyncResp->res.jsonValue["Name"] = "OpenBmc Manager";
19637e860f15SJohn Edward Broadbent         asyncResp->res.jsonValue["Description"] =
19647e860f15SJohn Edward Broadbent             "Baseboard Management Controller";
19657e860f15SJohn Edward Broadbent         asyncResp->res.jsonValue["PowerState"] = "On";
19661476687dSEd Tanous         asyncResp->res.jsonValue["Status"]["State"] = "Enabled";
19671476687dSEd Tanous         asyncResp->res.jsonValue["Status"]["Health"] = "OK";
19681476687dSEd Tanous 
19697e860f15SJohn Edward Broadbent         asyncResp->res.jsonValue["ManagerType"] = "BMC";
19707e860f15SJohn Edward Broadbent         asyncResp->res.jsonValue["UUID"] = systemd_utils::getUuid();
19717e860f15SJohn Edward Broadbent         asyncResp->res.jsonValue["ServiceEntryPointUUID"] = uuid;
1972002d39b4SEd Tanous         asyncResp->res.jsonValue["Model"] = "OpenBmc"; // TODO(ed), get model
19737e860f15SJohn Edward Broadbent 
19741476687dSEd Tanous         asyncResp->res.jsonValue["LogServices"]["@odata.id"] =
19751476687dSEd Tanous             "/redfish/v1/Managers/bmc/LogServices";
19761476687dSEd Tanous         asyncResp->res.jsonValue["NetworkProtocol"]["@odata.id"] =
19771476687dSEd Tanous             "/redfish/v1/Managers/bmc/NetworkProtocol";
19781476687dSEd Tanous         asyncResp->res.jsonValue["EthernetInterfaces"]["@odata.id"] =
19791476687dSEd Tanous             "/redfish/v1/Managers/bmc/EthernetInterfaces";
19807e860f15SJohn Edward Broadbent 
19817e860f15SJohn Edward Broadbent #ifdef BMCWEB_ENABLE_VM_NBDPROXY
19821476687dSEd Tanous         asyncResp->res.jsonValue["VirtualMedia"]["@odata.id"] =
19831476687dSEd Tanous             "/redfish/v1/Managers/bmc/VirtualMedia";
19847e860f15SJohn Edward Broadbent #endif // BMCWEB_ENABLE_VM_NBDPROXY
19857e860f15SJohn Edward Broadbent 
19867e860f15SJohn Edward Broadbent         // default oem data
19877e860f15SJohn Edward Broadbent         nlohmann::json& oem = asyncResp->res.jsonValue["Oem"];
19887e860f15SJohn Edward Broadbent         nlohmann::json& oemOpenbmc = oem["OpenBmc"];
19897e860f15SJohn Edward Broadbent         oem["@odata.type"] = "#OemManager.Oem";
19907e860f15SJohn Edward Broadbent         oem["@odata.id"] = "/redfish/v1/Managers/bmc#/Oem";
19917e860f15SJohn Edward Broadbent         oemOpenbmc["@odata.type"] = "#OemManager.OpenBmc";
19927e860f15SJohn Edward Broadbent         oemOpenbmc["@odata.id"] = "/redfish/v1/Managers/bmc#/Oem/OpenBmc";
19931476687dSEd Tanous 
19941476687dSEd Tanous         nlohmann::json::object_t certificates;
19951476687dSEd Tanous         certificates["@odata.id"] =
19961476687dSEd Tanous             "/redfish/v1/Managers/bmc/Truststore/Certificates";
19971476687dSEd Tanous         oemOpenbmc["Certificates"] = std::move(certificates);
19987e860f15SJohn Edward Broadbent 
19997e860f15SJohn Edward Broadbent         // Manager.Reset (an action) can be many values, OpenBMC only
20007e860f15SJohn Edward Broadbent         // supports BMC reboot.
20017e860f15SJohn Edward Broadbent         nlohmann::json& managerReset =
20027e860f15SJohn Edward Broadbent             asyncResp->res.jsonValue["Actions"]["#Manager.Reset"];
20037e860f15SJohn Edward Broadbent         managerReset["target"] =
20047e860f15SJohn Edward Broadbent             "/redfish/v1/Managers/bmc/Actions/Manager.Reset";
20057e860f15SJohn Edward Broadbent         managerReset["@Redfish.ActionInfo"] =
20067e860f15SJohn Edward Broadbent             "/redfish/v1/Managers/bmc/ResetActionInfo";
20077e860f15SJohn Edward Broadbent 
20087e860f15SJohn Edward Broadbent         // ResetToDefaults (Factory Reset) has values like
20097e860f15SJohn Edward Broadbent         // PreserveNetworkAndUsers and PreserveNetwork that aren't supported
20107e860f15SJohn Edward Broadbent         // on OpenBMC
20117e860f15SJohn Edward Broadbent         nlohmann::json& resetToDefaults =
20127e860f15SJohn Edward Broadbent             asyncResp->res.jsonValue["Actions"]["#Manager.ResetToDefaults"];
20137e860f15SJohn Edward Broadbent         resetToDefaults["target"] =
20147e860f15SJohn Edward Broadbent             "/redfish/v1/Managers/bmc/Actions/Manager.ResetToDefaults";
20157e860f15SJohn Edward Broadbent         resetToDefaults["ResetType@Redfish.AllowableValues"] = {"ResetAll"};
20167e860f15SJohn Edward Broadbent 
20177c8c4058STejas Patil         std::pair<std::string, std::string> redfishDateTimeOffset =
20187c8c4058STejas Patil             crow::utility::getDateTimeOffsetNow();
20197c8c4058STejas Patil 
20207c8c4058STejas Patil         asyncResp->res.jsonValue["DateTime"] = redfishDateTimeOffset.first;
20217c8c4058STejas Patil         asyncResp->res.jsonValue["DateTimeLocalOffset"] =
20227c8c4058STejas Patil             redfishDateTimeOffset.second;
20237e860f15SJohn Edward Broadbent 
20240e8ac5e7SGunnar Mills         // TODO (Gunnar): Remove these one day since moved to ComputerSystem
20250e8ac5e7SGunnar Mills         // Still used by OCP profiles
20260e8ac5e7SGunnar Mills         // https://github.com/opencomputeproject/OCP-Profiles/issues/23
20277e860f15SJohn Edward Broadbent         // Fill in SerialConsole info
20287e860f15SJohn Edward Broadbent         asyncResp->res.jsonValue["SerialConsole"]["ServiceEnabled"] = true;
2029002d39b4SEd Tanous         asyncResp->res.jsonValue["SerialConsole"]["MaxConcurrentSessions"] = 15;
2030002d39b4SEd Tanous         asyncResp->res.jsonValue["SerialConsole"]["ConnectTypesSupported"] = {
2031002d39b4SEd Tanous             "IPMI", "SSH"};
20327e860f15SJohn Edward Broadbent #ifdef BMCWEB_ENABLE_KVM
20337e860f15SJohn Edward Broadbent         // Fill in GraphicalConsole info
2034002d39b4SEd Tanous         asyncResp->res.jsonValue["GraphicalConsole"]["ServiceEnabled"] = true;
2035002d39b4SEd Tanous         asyncResp->res.jsonValue["GraphicalConsole"]["MaxConcurrentSessions"] =
2036002d39b4SEd Tanous             4;
20377e860f15SJohn Edward Broadbent         asyncResp->res
2038002d39b4SEd Tanous             .jsonValue["GraphicalConsole"]["ConnectTypesSupported"] = {"KVMIP"};
20397e860f15SJohn Edward Broadbent #endif // BMCWEB_ENABLE_KVM
20407e860f15SJohn Edward Broadbent 
2041002d39b4SEd Tanous         asyncResp->res.jsonValue["Links"]["ManagerForServers@odata.count"] = 1;
20421476687dSEd Tanous 
20431476687dSEd Tanous         nlohmann::json::array_t managerForServers;
20441476687dSEd Tanous         nlohmann::json::object_t manager;
20451476687dSEd Tanous         manager["@odata.id"] = "/redfish/v1/Systems/system";
20461476687dSEd Tanous         managerForServers.push_back(std::move(manager));
20471476687dSEd Tanous 
20481476687dSEd Tanous         asyncResp->res.jsonValue["Links"]["ManagerForServers"] =
20491476687dSEd Tanous             std::move(managerForServers);
20507e860f15SJohn Edward Broadbent 
20517e860f15SJohn Edward Broadbent         auto health = std::make_shared<HealthPopulate>(asyncResp);
20527e860f15SJohn Edward Broadbent         health->isManagersHealth = true;
20537e860f15SJohn Edward Broadbent         health->populate();
20547e860f15SJohn Edward Broadbent 
2055eee0013eSWilly Tu         sw_util::populateSoftwareInformation(asyncResp, sw_util::bmcPurpose,
20567e860f15SJohn Edward Broadbent                                              "FirmwareVersion", true);
20577e860f15SJohn Edward Broadbent 
20587e860f15SJohn Edward Broadbent         managerGetLastResetTime(asyncResp);
20597e860f15SJohn Edward Broadbent 
2060a51fc2d2SSui Chen         // ManagerDiagnosticData is added for all BMCs.
2061a51fc2d2SSui Chen         nlohmann::json& managerDiagnosticData =
2062a51fc2d2SSui Chen             asyncResp->res.jsonValue["ManagerDiagnosticData"];
2063a51fc2d2SSui Chen         managerDiagnosticData["@odata.id"] =
2064a51fc2d2SSui Chen             "/redfish/v1/Managers/bmc/ManagerDiagnosticData";
2065a51fc2d2SSui Chen 
20667e860f15SJohn Edward Broadbent         auto pids = std::make_shared<GetPIDValues>(asyncResp);
20677e860f15SJohn Edward Broadbent         pids->run();
20687e860f15SJohn Edward Broadbent 
2069002d39b4SEd Tanous         getMainChassisId(asyncResp,
2070002d39b4SEd Tanous                          [](const std::string& chassisId,
2071002d39b4SEd Tanous                             const std::shared_ptr<bmcweb::AsyncResp>& aRsp) {
2072002d39b4SEd Tanous             aRsp->res.jsonValue["Links"]["ManagerForChassis@odata.count"] = 1;
20731476687dSEd Tanous             nlohmann::json::array_t managerForChassis;
20748a592810SEd Tanous             nlohmann::json::object_t managerObj;
20758a592810SEd Tanous             managerObj["@odata.id"] = "/redfish/v1/Chassis/" + chassisId;
20768a592810SEd Tanous             managerForChassis.push_back(std::move(managerObj));
20771476687dSEd Tanous             aRsp->res.jsonValue["Links"]["ManagerForChassis"] =
20781476687dSEd Tanous                 std::move(managerForChassis);
20791476687dSEd Tanous             aRsp->res.jsonValue["Links"]["ManagerInChassis"]["@odata.id"] =
20801476687dSEd Tanous                 "/redfish/v1/Chassis/" + chassisId;
20817e860f15SJohn Edward Broadbent         });
20827e860f15SJohn Edward Broadbent 
20837e860f15SJohn Edward Broadbent         static bool started = false;
20847e860f15SJohn Edward Broadbent 
20857e860f15SJohn Edward Broadbent         if (!started)
20861abe55efSEd Tanous         {
20871e1e598dSJonathan Doman             sdbusplus::asio::getProperty<double>(
20881e1e598dSJonathan Doman                 *crow::connections::systemBus, "org.freedesktop.systemd1",
2089002d39b4SEd Tanous                 "/org/freedesktop/systemd1", "org.freedesktop.systemd1.Manager",
2090002d39b4SEd Tanous                 "Progress",
20917e860f15SJohn Edward Broadbent                 [asyncResp](const boost::system::error_code ec,
20921e1e598dSJonathan Doman                             const double& val) {
20937e860f15SJohn Edward Broadbent                 if (ec)
20941abe55efSEd Tanous                 {
20957e860f15SJohn Edward Broadbent                     BMCWEB_LOG_ERROR << "Error while getting progress";
20967e860f15SJohn Edward Broadbent                     messages::internalError(asyncResp->res);
20977e860f15SJohn Edward Broadbent                     return;
20987e860f15SJohn Edward Broadbent                 }
20991e1e598dSJonathan Doman                 if (val < 1.0)
21007e860f15SJohn Edward Broadbent                 {
2101002d39b4SEd Tanous                     asyncResp->res.jsonValue["Status"]["State"] = "Starting";
21027e860f15SJohn Edward Broadbent                     started = true;
21037e860f15SJohn Edward Broadbent                 }
21041e1e598dSJonathan Doman                 });
21059c310685SBorawski.Lukasz         }
21069c310685SBorawski.Lukasz 
21077e860f15SJohn Edward Broadbent         crow::connections::systemBus->async_method_call(
21087e860f15SJohn Edward Broadbent             [asyncResp](
21097e860f15SJohn Edward Broadbent                 const boost::system::error_code ec,
2110b9d36b47SEd Tanous                 const dbus::utility::MapperGetSubTreeResponse& subtree) {
21117e860f15SJohn Edward Broadbent             if (ec)
21121abe55efSEd Tanous             {
2113002d39b4SEd Tanous                 BMCWEB_LOG_DEBUG << "D-Bus response error on GetSubTree " << ec;
21147e860f15SJohn Edward Broadbent                 return;
21157e860f15SJohn Edward Broadbent             }
211626f6976fSEd Tanous             if (subtree.empty())
21177e860f15SJohn Edward Broadbent             {
21187e860f15SJohn Edward Broadbent                 BMCWEB_LOG_DEBUG << "Can't find bmc D-Bus object!";
21197e860f15SJohn Edward Broadbent                 return;
21207e860f15SJohn Edward Broadbent             }
21217e860f15SJohn Edward Broadbent             // Assume only 1 bmc D-Bus object
21227e860f15SJohn Edward Broadbent             // Throw an error if there is more than 1
21237e860f15SJohn Edward Broadbent             if (subtree.size() > 1)
21247e860f15SJohn Edward Broadbent             {
2125002d39b4SEd Tanous                 BMCWEB_LOG_DEBUG << "Found more than 1 bmc D-Bus object!";
21267e860f15SJohn Edward Broadbent                 messages::internalError(asyncResp->res);
21277e860f15SJohn Edward Broadbent                 return;
21287e860f15SJohn Edward Broadbent             }
21297e860f15SJohn Edward Broadbent 
2130002d39b4SEd Tanous             if (subtree[0].first.empty() || subtree[0].second.size() != 1)
21317e860f15SJohn Edward Broadbent             {
21327e860f15SJohn Edward Broadbent                 BMCWEB_LOG_DEBUG << "Error getting bmc D-Bus object!";
21337e860f15SJohn Edward Broadbent                 messages::internalError(asyncResp->res);
21347e860f15SJohn Edward Broadbent                 return;
21357e860f15SJohn Edward Broadbent             }
21367e860f15SJohn Edward Broadbent 
21377e860f15SJohn Edward Broadbent             const std::string& path = subtree[0].first;
2138002d39b4SEd Tanous             const std::string& connectionName = subtree[0].second[0].first;
21397e860f15SJohn Edward Broadbent 
2140002d39b4SEd Tanous             for (const auto& interfaceName : subtree[0].second[0].second)
21417e860f15SJohn Edward Broadbent             {
21427e860f15SJohn Edward Broadbent                 if (interfaceName ==
21437e860f15SJohn Edward Broadbent                     "xyz.openbmc_project.Inventory.Decorator.Asset")
21447e860f15SJohn Edward Broadbent                 {
21457e860f15SJohn Edward Broadbent                     crow::connections::systemBus->async_method_call(
21468a592810SEd Tanous                         [asyncResp](const boost::system::error_code ec2,
2147b9d36b47SEd Tanous                                     const dbus::utility::DBusPropertiesMap&
21487e860f15SJohn Edward Broadbent                                         propertiesList) {
21498a592810SEd Tanous                         if (ec2)
21507e860f15SJohn Edward Broadbent                         {
2151002d39b4SEd Tanous                             BMCWEB_LOG_DEBUG << "Can't get bmc asset!";
21527e860f15SJohn Edward Broadbent                             return;
21537e860f15SJohn Edward Broadbent                         }
2154002d39b4SEd Tanous                         for (const std::pair<std::string,
2155168e20c1SEd Tanous                                              dbus::utility::DbusVariantType>&
21567e860f15SJohn Edward Broadbent                                  property : propertiesList)
21577e860f15SJohn Edward Broadbent                         {
2158002d39b4SEd Tanous                             const std::string& propertyName = property.first;
21597e860f15SJohn Edward Broadbent 
21607e860f15SJohn Edward Broadbent                             if ((propertyName == "PartNumber") ||
21617e860f15SJohn Edward Broadbent                                 (propertyName == "SerialNumber") ||
21627e860f15SJohn Edward Broadbent                                 (propertyName == "Manufacturer") ||
21637e860f15SJohn Edward Broadbent                                 (propertyName == "Model") ||
21647e860f15SJohn Edward Broadbent                                 (propertyName == "SparePartNumber"))
21657e860f15SJohn Edward Broadbent                             {
21667e860f15SJohn Edward Broadbent                                 const std::string* value =
2167002d39b4SEd Tanous                                     std::get_if<std::string>(&property.second);
21687e860f15SJohn Edward Broadbent                                 if (value == nullptr)
21697e860f15SJohn Edward Broadbent                                 {
21707e860f15SJohn Edward Broadbent                                     // illegal property
2171002d39b4SEd Tanous                                     messages::internalError(asyncResp->res);
21727e860f15SJohn Edward Broadbent                                     return;
21737e860f15SJohn Edward Broadbent                                 }
2174002d39b4SEd Tanous                                 asyncResp->res.jsonValue[propertyName] = *value;
21757e860f15SJohn Edward Broadbent                             }
21767e860f15SJohn Edward Broadbent                         }
21777e860f15SJohn Edward Broadbent                         },
2178002d39b4SEd Tanous                         connectionName, path, "org.freedesktop.DBus.Properties",
2179002d39b4SEd Tanous                         "GetAll",
21800fda0f12SGeorge Liu                         "xyz.openbmc_project.Inventory.Decorator.Asset");
21817e860f15SJohn Edward Broadbent                 }
2182002d39b4SEd Tanous                 else if (interfaceName ==
21830fda0f12SGeorge Liu                          "xyz.openbmc_project.Inventory.Decorator.LocationCode")
21847e860f15SJohn Edward Broadbent                 {
21857e860f15SJohn Edward Broadbent                     getLocation(asyncResp, connectionName, path);
21867e860f15SJohn Edward Broadbent                 }
21877e860f15SJohn Edward Broadbent             }
21887e860f15SJohn Edward Broadbent             },
21897e860f15SJohn Edward Broadbent             "xyz.openbmc_project.ObjectMapper",
21907e860f15SJohn Edward Broadbent             "/xyz/openbmc_project/object_mapper",
21917e860f15SJohn Edward Broadbent             "xyz.openbmc_project.ObjectMapper", "GetSubTree",
21927e860f15SJohn Edward Broadbent             "/xyz/openbmc_project/inventory", int32_t(0),
21937e860f15SJohn Edward Broadbent             std::array<const char*, 1>{
21947e860f15SJohn Edward Broadbent                 "xyz.openbmc_project.Inventory.Item.Bmc"});
21957e860f15SJohn Edward Broadbent         });
21967e860f15SJohn Edward Broadbent 
21977e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/")
2198ed398213SEd Tanous         .privileges(redfish::privileges::patchManager)
219945ca1b86SEd Tanous         .methods(boost::beast::http::verb::patch)(
220045ca1b86SEd Tanous             [&app](const crow::Request& req,
22017e860f15SJohn Edward Broadbent                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
22023ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
220345ca1b86SEd Tanous         {
220445ca1b86SEd Tanous             return;
220545ca1b86SEd Tanous         }
22067e860f15SJohn Edward Broadbent         std::optional<nlohmann::json> oem;
22077e860f15SJohn Edward Broadbent         std::optional<nlohmann::json> links;
22087e860f15SJohn Edward Broadbent         std::optional<std::string> datetime;
22097e860f15SJohn Edward Broadbent 
221015ed6780SWilly Tu         if (!json_util::readJsonPatch(req, asyncResp->res, "Oem", oem,
2211002d39b4SEd Tanous                                       "DateTime", datetime, "Links", links))
22127e860f15SJohn Edward Broadbent         {
22137e860f15SJohn Edward Broadbent             return;
22147e860f15SJohn Edward Broadbent         }
22157e860f15SJohn Edward Broadbent 
22167e860f15SJohn Edward Broadbent         if (oem)
22177e860f15SJohn Edward Broadbent         {
22187e860f15SJohn Edward Broadbent             std::optional<nlohmann::json> openbmc;
2219002d39b4SEd Tanous             if (!redfish::json_util::readJson(*oem, asyncResp->res, "OpenBmc",
2220002d39b4SEd Tanous                                               openbmc))
22217e860f15SJohn Edward Broadbent             {
22227e860f15SJohn Edward Broadbent                 BMCWEB_LOG_ERROR
22237e860f15SJohn Edward Broadbent                     << "Illegal Property "
2224002d39b4SEd Tanous                     << oem->dump(2, ' ', true,
22257e860f15SJohn Edward Broadbent                                  nlohmann::json::error_handler_t::replace);
22267e860f15SJohn Edward Broadbent                 return;
22277e860f15SJohn Edward Broadbent             }
22287e860f15SJohn Edward Broadbent             if (openbmc)
22297e860f15SJohn Edward Broadbent             {
22307e860f15SJohn Edward Broadbent                 std::optional<nlohmann::json> fan;
2231002d39b4SEd Tanous                 if (!redfish::json_util::readJson(*openbmc, asyncResp->res,
2232002d39b4SEd Tanous                                                   "Fan", fan))
22337e860f15SJohn Edward Broadbent                 {
22347e860f15SJohn Edward Broadbent                     BMCWEB_LOG_ERROR
22357e860f15SJohn Edward Broadbent                         << "Illegal Property "
2236002d39b4SEd Tanous                         << openbmc->dump(
2237002d39b4SEd Tanous                                2, ' ', true,
2238002d39b4SEd Tanous                                nlohmann::json::error_handler_t::replace);
22397e860f15SJohn Edward Broadbent                     return;
22407e860f15SJohn Edward Broadbent                 }
22417e860f15SJohn Edward Broadbent                 if (fan)
22427e860f15SJohn Edward Broadbent                 {
2243002d39b4SEd Tanous                     auto pid = std::make_shared<SetPIDValues>(asyncResp, *fan);
22447e860f15SJohn Edward Broadbent                     pid->run();
22457e860f15SJohn Edward Broadbent                 }
22467e860f15SJohn Edward Broadbent             }
22477e860f15SJohn Edward Broadbent         }
22487e860f15SJohn Edward Broadbent         if (links)
22497e860f15SJohn Edward Broadbent         {
22507e860f15SJohn Edward Broadbent             std::optional<nlohmann::json> activeSoftwareImage;
22517e860f15SJohn Edward Broadbent             if (!redfish::json_util::readJson(*links, asyncResp->res,
22527e860f15SJohn Edward Broadbent                                               "ActiveSoftwareImage",
22537e860f15SJohn Edward Broadbent                                               activeSoftwareImage))
22547e860f15SJohn Edward Broadbent             {
22557e860f15SJohn Edward Broadbent                 return;
22567e860f15SJohn Edward Broadbent             }
22577e860f15SJohn Edward Broadbent             if (activeSoftwareImage)
22587e860f15SJohn Edward Broadbent             {
22597e860f15SJohn Edward Broadbent                 std::optional<std::string> odataId;
2260002d39b4SEd Tanous                 if (!json_util::readJson(*activeSoftwareImage, asyncResp->res,
2261002d39b4SEd Tanous                                          "@odata.id", odataId))
22627e860f15SJohn Edward Broadbent                 {
22637e860f15SJohn Edward Broadbent                     return;
22647e860f15SJohn Edward Broadbent                 }
22657e860f15SJohn Edward Broadbent 
22667e860f15SJohn Edward Broadbent                 if (odataId)
22677e860f15SJohn Edward Broadbent                 {
22687e860f15SJohn Edward Broadbent                     setActiveFirmwareImage(asyncResp, *odataId);
22697e860f15SJohn Edward Broadbent                 }
22707e860f15SJohn Edward Broadbent             }
22717e860f15SJohn Edward Broadbent         }
22727e860f15SJohn Edward Broadbent         if (datetime)
22737e860f15SJohn Edward Broadbent         {
22747e860f15SJohn Edward Broadbent             setDateTime(asyncResp, std::move(*datetime));
22757e860f15SJohn Edward Broadbent         }
22767e860f15SJohn Edward Broadbent         });
22777e860f15SJohn Edward Broadbent }
22787e860f15SJohn Edward Broadbent 
22797e860f15SJohn Edward Broadbent inline void requestRoutesManagerCollection(App& app)
22807e860f15SJohn Edward Broadbent {
22817e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/Managers/")
2282ed398213SEd Tanous         .privileges(redfish::privileges::getManagerCollection)
22837e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
228445ca1b86SEd Tanous             [&app](const crow::Request& req,
22857e860f15SJohn Edward Broadbent                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
22863ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
228745ca1b86SEd Tanous         {
228845ca1b86SEd Tanous             return;
228945ca1b86SEd Tanous         }
229083ff9ab6SJames Feist         // Collections don't include the static data added by SubRoute
229183ff9ab6SJames Feist         // because it has a duplicate entry for members
22928d1b46d7Szhanghch05         asyncResp->res.jsonValue["@odata.id"] = "/redfish/v1/Managers";
22938d1b46d7Szhanghch05         asyncResp->res.jsonValue["@odata.type"] =
22948d1b46d7Szhanghch05             "#ManagerCollection.ManagerCollection";
22958d1b46d7Szhanghch05         asyncResp->res.jsonValue["Name"] = "Manager Collection";
22968d1b46d7Szhanghch05         asyncResp->res.jsonValue["Members@odata.count"] = 1;
22971476687dSEd Tanous         nlohmann::json::array_t members;
22981476687dSEd Tanous         nlohmann::json& bmc = members.emplace_back();
22991476687dSEd Tanous         bmc["@odata.id"] = "/redfish/v1/Managers/bmc";
23001476687dSEd Tanous         asyncResp->res.jsonValue["Members"] = std::move(members);
23017e860f15SJohn Edward Broadbent         });
23029c310685SBorawski.Lukasz }
23039c310685SBorawski.Lukasz } // namespace redfish
2304