xref: /openbmc/bmcweb/features/redfish/lib/power.hpp (revision 1b1b43f20a1f52d014f6435232c19c1d9c8fa337)
12474adfaSEd Tanous /*
22474adfaSEd Tanous // Copyright (c) 2018 Intel Corporation
32474adfaSEd Tanous // Copyright (c) 2018 Ampere Computing LLC
42474adfaSEd Tanous /
52474adfaSEd Tanous // Licensed under the Apache License, Version 2.0 (the "License");
62474adfaSEd Tanous // you may not use this file except in compliance with the License.
72474adfaSEd Tanous // You may obtain a copy of the License at
82474adfaSEd Tanous //
92474adfaSEd Tanous //      http://www.apache.org/licenses/LICENSE-2.0
102474adfaSEd Tanous //
112474adfaSEd Tanous // Unless required by applicable law or agreed to in writing, software
122474adfaSEd Tanous // distributed under the License is distributed on an "AS IS" BASIS,
132474adfaSEd Tanous // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
142474adfaSEd Tanous // See the License for the specific language governing permissions and
152474adfaSEd Tanous // limitations under the License.
162474adfaSEd Tanous */
172474adfaSEd Tanous #pragma once
182474adfaSEd Tanous 
192474adfaSEd Tanous #include "node.hpp"
202474adfaSEd Tanous #include "sensors.hpp"
212474adfaSEd Tanous 
222474adfaSEd Tanous namespace redfish
232474adfaSEd Tanous {
242474adfaSEd Tanous 
252474adfaSEd Tanous class Power : public Node
262474adfaSEd Tanous {
272474adfaSEd Tanous   public:
282474adfaSEd Tanous     Power(CrowApp& app) :
292474adfaSEd Tanous         Node((app), "/redfish/v1/Chassis/<str>/Power/", std::string())
302474adfaSEd Tanous     {
312474adfaSEd Tanous         entityPrivileges = {
322474adfaSEd Tanous             {boost::beast::http::verb::get, {{"Login"}}},
332474adfaSEd Tanous             {boost::beast::http::verb::head, {{"Login"}}},
34*1b1b43f2Sjayaprakash Mutyala             {boost::beast::http::verb::patch, {{"ConfigureManager"}}},
352474adfaSEd Tanous             {boost::beast::http::verb::put, {{"ConfigureManager"}}},
362474adfaSEd Tanous             {boost::beast::http::verb::delete_, {{"ConfigureManager"}}},
372474adfaSEd Tanous             {boost::beast::http::verb::post, {{"ConfigureManager"}}}};
382474adfaSEd Tanous     }
392474adfaSEd Tanous 
402474adfaSEd Tanous   private:
4185e1424fSEd Tanous     std::vector<const char*> typeList = {"/xyz/openbmc_project/sensors/voltage",
42413961deSRichard Marian Thomaiyar                                          "/xyz/openbmc_project/sensors/power"};
434bb3dc34SCarol Wang     void setPowerCapOverride(
444bb3dc34SCarol Wang         std::shared_ptr<SensorsAsyncResp> asyncResp,
454bb3dc34SCarol Wang         std::vector<nlohmann::json>& powerControlCollections)
464bb3dc34SCarol Wang     {
474bb3dc34SCarol Wang         auto getChassisPath =
484bb3dc34SCarol Wang             [asyncResp, powerControlCollections](
494bb3dc34SCarol Wang                 const std::optional<std::string>& chassisPath) mutable {
504bb3dc34SCarol Wang                 if (!chassisPath)
514bb3dc34SCarol Wang                 {
524bb3dc34SCarol Wang                     BMCWEB_LOG_ERROR << "Don't find valid chassis path ";
534bb3dc34SCarol Wang                     messages::resourceNotFound(asyncResp->res, "Chassis",
544bb3dc34SCarol Wang                                                asyncResp->chassisId);
554bb3dc34SCarol Wang                     return;
564bb3dc34SCarol Wang                 }
574bb3dc34SCarol Wang 
584bb3dc34SCarol Wang                 if (powerControlCollections.size() != 1)
594bb3dc34SCarol Wang                 {
604bb3dc34SCarol Wang                     BMCWEB_LOG_ERROR
614bb3dc34SCarol Wang                         << "Don't support multiple hosts at present ";
624bb3dc34SCarol Wang                     messages::resourceNotFound(asyncResp->res, "Power",
634bb3dc34SCarol Wang                                                "PowerControl");
644bb3dc34SCarol Wang                     return;
654bb3dc34SCarol Wang                 }
664bb3dc34SCarol Wang 
674bb3dc34SCarol Wang                 auto& item = powerControlCollections[0];
684bb3dc34SCarol Wang 
694bb3dc34SCarol Wang                 std::optional<nlohmann::json> powerLimit;
704bb3dc34SCarol Wang                 if (!json_util::readJson(item, asyncResp->res, "PowerLimit",
714bb3dc34SCarol Wang                                          powerLimit))
724bb3dc34SCarol Wang                 {
734bb3dc34SCarol Wang                     return;
744bb3dc34SCarol Wang                 }
754bb3dc34SCarol Wang                 if (!powerLimit)
764bb3dc34SCarol Wang                 {
774bb3dc34SCarol Wang                     return;
784bb3dc34SCarol Wang                 }
794bb3dc34SCarol Wang                 std::optional<uint32_t> value;
804bb3dc34SCarol Wang                 if (!json_util::readJson(*powerLimit, asyncResp->res,
814bb3dc34SCarol Wang                                          "LimitInWatts", value))
824bb3dc34SCarol Wang                 {
834bb3dc34SCarol Wang                     return;
844bb3dc34SCarol Wang                 }
854bb3dc34SCarol Wang                 if (!value)
864bb3dc34SCarol Wang                 {
874bb3dc34SCarol Wang                     return;
884bb3dc34SCarol Wang                 }
894bb3dc34SCarol Wang                 auto valueHandler = [value, asyncResp](
904bb3dc34SCarol Wang                                         const boost::system::error_code ec,
914bb3dc34SCarol Wang                                         const SensorVariant& powerCapEnable) {
924bb3dc34SCarol Wang                     if (ec)
934bb3dc34SCarol Wang                     {
944bb3dc34SCarol Wang                         messages::internalError(asyncResp->res);
954bb3dc34SCarol Wang                         BMCWEB_LOG_ERROR
964bb3dc34SCarol Wang                             << "powerCapEnable Get handler: Dbus error " << ec;
974bb3dc34SCarol Wang                         return;
984bb3dc34SCarol Wang                     }
994bb3dc34SCarol Wang                     // Check PowerCapEnable
1004bb3dc34SCarol Wang                     const bool* b =
1014bb3dc34SCarol Wang                         sdbusplus::message::variant_ns::get_if<bool>(
1024bb3dc34SCarol Wang                             &powerCapEnable);
1034bb3dc34SCarol Wang                     if (b == nullptr)
1044bb3dc34SCarol Wang                     {
1054bb3dc34SCarol Wang                         messages::internalError(asyncResp->res);
1064bb3dc34SCarol Wang                         BMCWEB_LOG_ERROR
1074bb3dc34SCarol Wang                             << "Fail to get PowerCapEnable status ";
1084bb3dc34SCarol Wang                         return;
1094bb3dc34SCarol Wang                     }
1104bb3dc34SCarol Wang                     if (!(*b))
1114bb3dc34SCarol Wang                     {
1124bb3dc34SCarol Wang                         messages::actionNotSupported(
1134bb3dc34SCarol Wang                             asyncResp->res,
1144bb3dc34SCarol Wang                             "Setting LimitInWatts when PowerLimit "
1154bb3dc34SCarol Wang                             "feature is disabled");
1164bb3dc34SCarol Wang                         BMCWEB_LOG_ERROR << "PowerLimit feature is disabled ";
1174bb3dc34SCarol Wang                         return;
1184bb3dc34SCarol Wang                     }
1194bb3dc34SCarol Wang 
1204bb3dc34SCarol Wang                     crow::connections::systemBus->async_method_call(
1214bb3dc34SCarol Wang                         [asyncResp](const boost::system::error_code ec) {
1224bb3dc34SCarol Wang                             if (ec)
1234bb3dc34SCarol Wang                             {
1244bb3dc34SCarol Wang                                 BMCWEB_LOG_DEBUG
1254bb3dc34SCarol Wang                                     << "Power Limit Set: Dbus error: " << ec;
1264bb3dc34SCarol Wang                                 messages::internalError(asyncResp->res);
1274bb3dc34SCarol Wang                                 return;
1284bb3dc34SCarol Wang                             }
1294bb3dc34SCarol Wang                             asyncResp->res.result(
1304bb3dc34SCarol Wang                                 boost::beast::http::status::no_content);
1314bb3dc34SCarol Wang                         },
1324bb3dc34SCarol Wang                         "xyz.openbmc_project.Settings",
1334bb3dc34SCarol Wang                         "/xyz/openbmc_project/control/host0/power_cap",
1344bb3dc34SCarol Wang                         "org.freedesktop.DBus.Properties", "Set",
1354bb3dc34SCarol Wang                         "xyz.openbmc_project.Control.Power.Cap", "PowerCap",
1364bb3dc34SCarol Wang                         sdbusplus::message::variant<uint32_t>(*value));
1374bb3dc34SCarol Wang                 };
1384bb3dc34SCarol Wang                 crow::connections::systemBus->async_method_call(
1394bb3dc34SCarol Wang                     std::move(valueHandler), "xyz.openbmc_project.Settings",
1404bb3dc34SCarol Wang                     "/xyz/openbmc_project/control/host0/power_cap",
1414bb3dc34SCarol Wang                     "org.freedesktop.DBus.Properties", "Get",
1424bb3dc34SCarol Wang                     "xyz.openbmc_project.Control.Power.Cap", "PowerCapEnable");
1434bb3dc34SCarol Wang             };
1444bb3dc34SCarol Wang         getValidChassisPath(asyncResp, std::move(getChassisPath));
1454bb3dc34SCarol Wang     }
1462474adfaSEd Tanous     void doGet(crow::Response& res, const crow::Request& req,
1472474adfaSEd Tanous                const std::vector<std::string>& params) override
1482474adfaSEd Tanous     {
1492474adfaSEd Tanous         if (params.size() != 1)
1502474adfaSEd Tanous         {
1512474adfaSEd Tanous             res.result(boost::beast::http::status::internal_server_error);
1522474adfaSEd Tanous             res.end();
1532474adfaSEd Tanous             return;
1542474adfaSEd Tanous         }
1552474adfaSEd Tanous         const std::string& chassis_name = params[0];
1562474adfaSEd Tanous 
157c5d03ff4SJennifer Lee         res.jsonValue["PowerControl"] = nlohmann::json::array();
158c5d03ff4SJennifer Lee 
1592474adfaSEd Tanous         auto sensorAsyncResp = std::make_shared<SensorsAsyncResp>(
160413961deSRichard Marian Thomaiyar             res, chassis_name, typeList, "Power");
161028f7ebcSEddie James 
1622474adfaSEd Tanous         getChassisData(sensorAsyncResp);
163028f7ebcSEddie James 
164028f7ebcSEddie James         // This callback verifies that the power limit is only provided for the
165028f7ebcSEddie James         // chassis that implements the Chassis inventory item. This prevents
166028f7ebcSEddie James         // things like power supplies providing the chassis power limit
167028f7ebcSEddie James         auto chassisHandler = [sensorAsyncResp](
168271584abSEd Tanous                                   const boost::system::error_code e,
169028f7ebcSEddie James                                   const std::vector<std::string>&
170028f7ebcSEddie James                                       chassisPaths) {
171271584abSEd Tanous             if (e)
172028f7ebcSEddie James             {
173028f7ebcSEddie James                 BMCWEB_LOG_ERROR
174271584abSEd Tanous                     << "Power Limit GetSubTreePaths handler Dbus error " << e;
175028f7ebcSEddie James                 return;
176028f7ebcSEddie James             }
177028f7ebcSEddie James 
178028f7ebcSEddie James             bool found = false;
179028f7ebcSEddie James             for (const std::string& chassis : chassisPaths)
180028f7ebcSEddie James             {
181028f7ebcSEddie James                 size_t len = std::string::npos;
182028f7ebcSEddie James                 size_t lastPos = chassis.rfind("/");
183028f7ebcSEddie James                 if (lastPos == std::string::npos)
184028f7ebcSEddie James                 {
185028f7ebcSEddie James                     continue;
186028f7ebcSEddie James                 }
187028f7ebcSEddie James 
188028f7ebcSEddie James                 if (lastPos == chassis.size() - 1)
189028f7ebcSEddie James                 {
190028f7ebcSEddie James                     size_t end = lastPos;
191028f7ebcSEddie James                     lastPos = chassis.rfind("/", lastPos - 1);
192028f7ebcSEddie James                     if (lastPos == std::string::npos)
193028f7ebcSEddie James                     {
194028f7ebcSEddie James                         continue;
195028f7ebcSEddie James                     }
196028f7ebcSEddie James 
197028f7ebcSEddie James                     len = end - (lastPos + 1);
198028f7ebcSEddie James                 }
199028f7ebcSEddie James 
200028f7ebcSEddie James                 std::string interfaceChassisName =
201028f7ebcSEddie James                     chassis.substr(lastPos + 1, len);
202028f7ebcSEddie James                 if (!interfaceChassisName.compare(sensorAsyncResp->chassisId))
203028f7ebcSEddie James                 {
204028f7ebcSEddie James                     found = true;
205028f7ebcSEddie James                     break;
206028f7ebcSEddie James                 }
207028f7ebcSEddie James             }
208028f7ebcSEddie James 
209028f7ebcSEddie James             if (!found)
210028f7ebcSEddie James             {
211028f7ebcSEddie James                 BMCWEB_LOG_DEBUG << "Power Limit not present for "
212028f7ebcSEddie James                                  << sensorAsyncResp->chassisId;
213028f7ebcSEddie James                 return;
214028f7ebcSEddie James             }
215028f7ebcSEddie James 
216028f7ebcSEddie James             auto valueHandler =
217028f7ebcSEddie James                 [sensorAsyncResp](
218028f7ebcSEddie James                     const boost::system::error_code ec,
219028f7ebcSEddie James                     const std::vector<std::pair<std::string, SensorVariant>>&
220028f7ebcSEddie James                         properties) {
221028f7ebcSEddie James                     if (ec)
222028f7ebcSEddie James                     {
223028f7ebcSEddie James                         messages::internalError(sensorAsyncResp->res);
224028f7ebcSEddie James                         BMCWEB_LOG_ERROR
225028f7ebcSEddie James                             << "Power Limit GetAll handler: Dbus error " << ec;
226028f7ebcSEddie James                         return;
227028f7ebcSEddie James                     }
228028f7ebcSEddie James 
229028f7ebcSEddie James                     nlohmann::json& tempArray =
2307ab06f49SGunnar Mills                         sensorAsyncResp->res.jsonValue["PowerControl"];
231028f7ebcSEddie James 
2327ab06f49SGunnar Mills                     // Put multiple "sensors" into a single PowerControl, 0, so
2337ab06f49SGunnar Mills                     // only create the first one
234028f7ebcSEddie James                     if (tempArray.empty())
235028f7ebcSEddie James                     {
2367ab06f49SGunnar Mills                         // Mandatory properties odata.id and MemberId
2377ab06f49SGunnar Mills                         // A warning without a odata.type
2387ab06f49SGunnar Mills                         tempArray.push_back(
2397ab06f49SGunnar Mills                             {{"@odata.type", "#Power.v1_0_0.PowerControl"},
2407ab06f49SGunnar Mills                              {"@odata.id", "/redfish/v1/Chassis/" +
2417ab06f49SGunnar Mills                                                sensorAsyncResp->chassisId +
2427ab06f49SGunnar Mills                                                "/Power#/PowerControl/0"},
2437ab06f49SGunnar Mills                              {"Name", "Chassis Power Control"},
2447ab06f49SGunnar Mills                              {"MemberId", "0"}});
245028f7ebcSEddie James                     }
246028f7ebcSEddie James 
247028f7ebcSEddie James                     nlohmann::json& sensorJson = tempArray.back();
248028f7ebcSEddie James                     bool enabled = false;
249028f7ebcSEddie James                     double powerCap = 0.0;
250028f7ebcSEddie James                     int64_t scale = 0;
251028f7ebcSEddie James 
252028f7ebcSEddie James                     for (const std::pair<std::string, SensorVariant>& property :
253028f7ebcSEddie James                          properties)
254028f7ebcSEddie James                     {
255028f7ebcSEddie James                         if (!property.first.compare("Scale"))
256028f7ebcSEddie James                         {
257028f7ebcSEddie James                             const int64_t* i =
258028f7ebcSEddie James                                 sdbusplus::message::variant_ns::get_if<int64_t>(
259028f7ebcSEddie James                                     &property.second);
260028f7ebcSEddie James 
261028f7ebcSEddie James                             if (i)
262028f7ebcSEddie James                             {
263028f7ebcSEddie James                                 scale = *i;
264028f7ebcSEddie James                             }
265028f7ebcSEddie James                         }
266028f7ebcSEddie James                         else if (!property.first.compare("PowerCap"))
267028f7ebcSEddie James                         {
268028f7ebcSEddie James                             const double* d =
269028f7ebcSEddie James                                 sdbusplus::message::variant_ns::get_if<double>(
270028f7ebcSEddie James                                     &property.second);
271028f7ebcSEddie James                             const int64_t* i =
272028f7ebcSEddie James                                 sdbusplus::message::variant_ns::get_if<int64_t>(
273028f7ebcSEddie James                                     &property.second);
274028f7ebcSEddie James                             const uint32_t* u =
275028f7ebcSEddie James                                 sdbusplus::message::variant_ns::get_if<
276028f7ebcSEddie James                                     uint32_t>(&property.second);
277028f7ebcSEddie James 
278028f7ebcSEddie James                             if (d)
279028f7ebcSEddie James                             {
280028f7ebcSEddie James                                 powerCap = *d;
281028f7ebcSEddie James                             }
282028f7ebcSEddie James                             else if (i)
283028f7ebcSEddie James                             {
284271584abSEd Tanous                                 powerCap = static_cast<double>(*i);
285028f7ebcSEddie James                             }
286028f7ebcSEddie James                             else if (u)
287028f7ebcSEddie James                             {
288028f7ebcSEddie James                                 powerCap = *u;
289028f7ebcSEddie James                             }
290028f7ebcSEddie James                         }
291028f7ebcSEddie James                         else if (!property.first.compare("PowerCapEnable"))
292028f7ebcSEddie James                         {
293028f7ebcSEddie James                             const bool* b =
294028f7ebcSEddie James                                 sdbusplus::message::variant_ns::get_if<bool>(
295028f7ebcSEddie James                                     &property.second);
296028f7ebcSEddie James 
297028f7ebcSEddie James                             if (b)
298028f7ebcSEddie James                             {
299028f7ebcSEddie James                                 enabled = *b;
300028f7ebcSEddie James                             }
301028f7ebcSEddie James                         }
302028f7ebcSEddie James                     }
303028f7ebcSEddie James 
3047ab06f49SGunnar Mills                     nlohmann::json& value =
3057ab06f49SGunnar Mills                         sensorJson["PowerLimit"]["LimitInWatts"];
306028f7ebcSEddie James 
3075a64a6f3SJoshi-Mansi                     // LimitException is Mandatory attribute as per OCP Baseline
3085a64a6f3SJoshi-Mansi                     // Profile – v1.0.0, so currently making it "NoAction"
3095a64a6f3SJoshi-Mansi                     // as default value to make it OCP Compliant.
3105a64a6f3SJoshi-Mansi                     sensorJson["PowerLimit"]["LimitException"] = "NoAction";
3115a64a6f3SJoshi-Mansi 
312028f7ebcSEddie James                     if (enabled)
313028f7ebcSEddie James                     {
314028f7ebcSEddie James                         // Redfish specification indicates PowerLimit should be
315028f7ebcSEddie James                         // null if the limit is not enabled.
316028f7ebcSEddie James                         value = powerCap * std::pow(10, scale);
317028f7ebcSEddie James                     }
318028f7ebcSEddie James                 };
319028f7ebcSEddie James 
320028f7ebcSEddie James             crow::connections::systemBus->async_method_call(
321028f7ebcSEddie James                 std::move(valueHandler), "xyz.openbmc_project.Settings",
322028f7ebcSEddie James                 "/xyz/openbmc_project/control/host0/power_cap",
323028f7ebcSEddie James                 "org.freedesktop.DBus.Properties", "GetAll",
324028f7ebcSEddie James                 "xyz.openbmc_project.Control.Power.Cap");
325028f7ebcSEddie James         };
326028f7ebcSEddie James 
327028f7ebcSEddie James         crow::connections::systemBus->async_method_call(
328028f7ebcSEddie James             std::move(chassisHandler), "xyz.openbmc_project.ObjectMapper",
329028f7ebcSEddie James             "/xyz/openbmc_project/object_mapper",
330028f7ebcSEddie James             "xyz.openbmc_project.ObjectMapper", "GetSubTreePaths",
331271584abSEd Tanous             "/xyz/openbmc_project/inventory", 0,
332f857e9aeSAppaRao Puli             std::array<const char*, 2>{
333f857e9aeSAppaRao Puli                 "xyz.openbmc_project.Inventory.Item.Board",
334028f7ebcSEddie James                 "xyz.openbmc_project.Inventory.Item.Chassis"});
3352474adfaSEd Tanous     }
336413961deSRichard Marian Thomaiyar     void doPatch(crow::Response& res, const crow::Request& req,
337413961deSRichard Marian Thomaiyar                  const std::vector<std::string>& params) override
338413961deSRichard Marian Thomaiyar     {
3394bb3dc34SCarol Wang         if (params.size() != 1)
3404bb3dc34SCarol Wang         {
3414bb3dc34SCarol Wang             messages::internalError(res);
3424bb3dc34SCarol Wang             res.end();
3434bb3dc34SCarol Wang             return;
3444bb3dc34SCarol Wang         }
3454bb3dc34SCarol Wang 
3464bb3dc34SCarol Wang         const std::string& chassisName = params[0];
3474bb3dc34SCarol Wang         auto asyncResp = std::make_shared<SensorsAsyncResp>(res, chassisName,
3484bb3dc34SCarol Wang                                                             typeList, "Power");
3494bb3dc34SCarol Wang 
3504bb3dc34SCarol Wang         std::optional<std::vector<nlohmann::json>> voltageCollections;
3514bb3dc34SCarol Wang         std::optional<std::vector<nlohmann::json>> powerCtlCollections;
3524bb3dc34SCarol Wang 
3534bb3dc34SCarol Wang         if (!json_util::readJson(req, asyncResp->res, "PowerControl",
3544bb3dc34SCarol Wang                                  powerCtlCollections, "Voltages",
3554bb3dc34SCarol Wang                                  voltageCollections))
3564bb3dc34SCarol Wang         {
3574bb3dc34SCarol Wang             return;
3584bb3dc34SCarol Wang         }
3594bb3dc34SCarol Wang 
3604bb3dc34SCarol Wang         if (powerCtlCollections)
3614bb3dc34SCarol Wang         {
3624bb3dc34SCarol Wang             setPowerCapOverride(asyncResp, *powerCtlCollections);
3634bb3dc34SCarol Wang         }
3644bb3dc34SCarol Wang         if (voltageCollections)
3654bb3dc34SCarol Wang         {
3664bb3dc34SCarol Wang             std::unordered_map<std::string, std::vector<nlohmann::json>>
3674bb3dc34SCarol Wang                 allCollections;
3684bb3dc34SCarol Wang             allCollections.emplace("Voltages", *std::move(voltageCollections));
369397fd61fSjayaprakash Mutyala             checkAndDoSensorsOverride(asyncResp, allCollections);
3704bb3dc34SCarol Wang         }
371413961deSRichard Marian Thomaiyar     }
3722474adfaSEd Tanous };
3732474adfaSEd Tanous 
3742474adfaSEd Tanous } // namespace redfish
375