Redfish: Set the power cap

Set the PowerCap with redfish patch.

Tested:
Case 1: PowerCapEnable is false
$ curl -k -H "X-Auth-Token: $token" -X PUT -d '{"data":false}' https://$bmc/xyz/openbmc_project/control/host0/power_cap/attr/PowerCapEnable
$ curl -k -H "X-Auth-Token: $token"https://${bmc}/redfish/v1/Chassis/chassis/Power
{
  "@odata.context": "/redfish/v1/$metadata#Power.Power",
  "@odata.id": "/redfish/v1/Chassis/chassis/Power",
  "@odata.type": "#Power.v1_5_2.Power",
  "Id": "Power",
  "Name": "Power",
  "PowerControl": [
    {
      "@odata.id": "/redfish/v1/Chassis/chassis/Power#/PowerControl/0",
      "@odata.type": "#Power.v1_0_0.PowerControl",
      "MemberId": "0",
      "Name": "Chassis Power Control",
      "PowerLimit": {
        "LimitInWatts": null
      }
    }
  ],
  ...
}
$curl -k -H "X-Auth-Token: $token" https://${bmc}/redfish/v1/Chassis/chassis/Power -X PATCH -d '{"PowerControl":[{"PowerLimit":{"LimitInWatts":2004}}]}'
{
  "error": {
    "@Message.ExtendedInfo": [
      {
        "@odata.type": "/redfish/v1/$metadata#Message.v1_0_0.Message",
        "Message": "PowerCapEnable is false, can't set the PowerCap.",
        "MessageArgs": [],
        "MessageId": "Base.1.4.0.UnableToSetPowerCap",
        "Resolution": "Set PowerCapEnable to be true before setting PowerCap.",
        "Severity": "Warning"
      }
    ],
    "code": "Base.1.4.0.UnableToSetPowerCap",
    "message": "PowerCapEnable is false, can't set the PowerCap."
  }
}

Case 2: PowerCapEnable is true, PowerControl json only
$ curl -k -H "X-Auth-Token: $token" -X PUT -d '{"data":true}' https://$bmc/xyz/openbmc_project/control/host0/power_cap/attr/PowerCapEnable
$ curl -k -H "X-Auth-Token: $token"https://${bmc}/redfish/v1/Chassis/chassis/Power
{
  "@odata.context": "/redfish/v1/$metadata#Power.Power",
  "@odata.id": "/redfish/v1/Chassis/chassis/Power",
  "@odata.type": "#Power.v1_5_2.Power",
  "Id": "Power",
  "Name": "Power",
  "PowerControl": [
    {
      "@odata.id": "/redfish/v1/Chassis/chassis/Power#/PowerControl/0",
      "@odata.type": "#Power.v1_0_0.PowerControl",
      "MemberId": "0",
      "Name": "Chassis Power Control",
      "PowerLimit": {
        "LimitInWatts": 2001.0
      }
    }
  ],
  ...
}
$ curl -k -H "X-Auth-Token: $token"https://${bmc}/redfish/v1/Chassis/chassis/Power -X PATCH -d '{"PowerControl":[{"PowerLimit":{"LimitInWatts":2004}}]}' -v
...
< HTTP/1.1 204 No Content
...
$ curl -k -H "X-Auth-Token: $token" https://${bmc}/redfish/v1/Chassis/chassis/Power
{
  "@odata.context": "/redfish/v1/$metadata#Power.Power",
  "@odata.id": "/redfish/v1/Chassis/chassis/Power",
  "@odata.type": "#Power.v1_5_2.Power",
  "Id": "Power",
  "Name": "Power",
  "PowerControl": [
    {
      "@odata.id": "/redfish/v1/Chassis/chassis/Power#/PowerControl/0",
      "@odata.type": "#Power.v1_0_0.PowerControl",
      "MemberId": "0",
      "Name": "Chassis Power Control",
      "PowerLimit": {
        "LimitInWatts": 2004.0
      }
    }
  ],
  ...
}

Case 3: PowerCapEnable is true, PowerControl and Voltages json
$ curl -k -H "X-Auth-Token: $token" https://${bmc}/redfish/v1/Chassis/chassis/Power -X PATCH -d '{"PowerControl":[{"PowerLimit"{"LimitInWatts":2001}}], "Voltages": [{"MemberId" : "p0_vcs_voltage", "ReadingVolts":8}]}' -v
...
< HTTP/1.1 204 No Content
...

Case 4: Wrong chassis path
$ curl -k -H "X-Auth-Token: $token" https://${bmc}/redfish/v1/Chassis/chassi/Power -X PATCH -d '{"PowerControl":[{"PowerLimit":{"LimitInWatts":2001}}]}'
{
  "error": {
    "@Message.ExtendedInfo": [
      {
        "@odata.type": "/redfish/v1/$metadata#Message.v1_0_0.Message",
        "Message": "The requested resource of type Chassis named chassi was not found.",
        "MessageArgs": [
          "Chassis",
          "chassi"
        ],
        "MessageId": "Base.1.4.0.ResourceNotFound",
        "Resolution": "Provide a valid resource identifier and resubmit the request.",
        "Severity": "Critical"
      }
    ],
    "code": "Base.1.4.0.ResourceNotFound",
    "message": "The requested resource of type Chassis named chassi was not found."
  }
}

Signed-off-by: Carol Wang <wangkair@cn.ibm.com>
Change-Id: Ifabdf053005b31cf3e3539009a1ec20ce4d46d5b
diff --git a/redfish-core/lib/power.hpp b/redfish-core/lib/power.hpp
index eaea5ab..ac7503d 100644
--- a/redfish-core/lib/power.hpp
+++ b/redfish-core/lib/power.hpp
@@ -40,6 +40,109 @@
   private:
     std::vector<const char*> typeList = {"/xyz/openbmc_project/sensors/voltage",
                                          "/xyz/openbmc_project/sensors/power"};
+    void setPowerCapOverride(
+        std::shared_ptr<SensorsAsyncResp> asyncResp,
+        std::vector<nlohmann::json>& powerControlCollections)
+    {
+        auto getChassisPath =
+            [asyncResp, powerControlCollections](
+                const std::optional<std::string>& chassisPath) mutable {
+                if (!chassisPath)
+                {
+                    BMCWEB_LOG_ERROR << "Don't find valid chassis path ";
+                    messages::resourceNotFound(asyncResp->res, "Chassis",
+                                               asyncResp->chassisId);
+                    return;
+                }
+
+                if (powerControlCollections.size() != 1)
+                {
+                    BMCWEB_LOG_ERROR
+                        << "Don't support multiple hosts at present ";
+                    messages::resourceNotFound(asyncResp->res, "Power",
+                                               "PowerControl");
+                    return;
+                }
+
+                auto& item = powerControlCollections[0];
+
+                std::optional<nlohmann::json> powerLimit;
+                if (!json_util::readJson(item, asyncResp->res, "PowerLimit",
+                                         powerLimit))
+                {
+                    return;
+                }
+                if (!powerLimit)
+                {
+                    return;
+                }
+                std::optional<uint32_t> value;
+                if (!json_util::readJson(*powerLimit, asyncResp->res,
+                                         "LimitInWatts", value))
+                {
+                    return;
+                }
+                if (!value)
+                {
+                    return;
+                }
+                auto valueHandler = [value, asyncResp](
+                                        const boost::system::error_code ec,
+                                        const SensorVariant& powerCapEnable) {
+                    if (ec)
+                    {
+                        messages::internalError(asyncResp->res);
+                        BMCWEB_LOG_ERROR
+                            << "powerCapEnable Get handler: Dbus error " << ec;
+                        return;
+                    }
+                    // Check PowerCapEnable
+                    const bool* b =
+                        sdbusplus::message::variant_ns::get_if<bool>(
+                            &powerCapEnable);
+                    if (b == nullptr)
+                    {
+                        messages::internalError(asyncResp->res);
+                        BMCWEB_LOG_ERROR
+                            << "Fail to get PowerCapEnable status ";
+                        return;
+                    }
+                    if (!(*b))
+                    {
+                        messages::actionNotSupported(
+                            asyncResp->res,
+                            "Setting LimitInWatts when PowerLimit "
+                            "feature is disabled");
+                        BMCWEB_LOG_ERROR << "PowerLimit feature is disabled ";
+                        return;
+                    }
+
+                    crow::connections::systemBus->async_method_call(
+                        [asyncResp](const boost::system::error_code ec) {
+                            if (ec)
+                            {
+                                BMCWEB_LOG_DEBUG
+                                    << "Power Limit Set: Dbus error: " << ec;
+                                messages::internalError(asyncResp->res);
+                                return;
+                            }
+                            asyncResp->res.result(
+                                boost::beast::http::status::no_content);
+                        },
+                        "xyz.openbmc_project.Settings",
+                        "/xyz/openbmc_project/control/host0/power_cap",
+                        "org.freedesktop.DBus.Properties", "Set",
+                        "xyz.openbmc_project.Control.Power.Cap", "PowerCap",
+                        sdbusplus::message::variant<uint32_t>(*value));
+                };
+                crow::connections::systemBus->async_method_call(
+                    std::move(valueHandler), "xyz.openbmc_project.Settings",
+                    "/xyz/openbmc_project/control/host0/power_cap",
+                    "org.freedesktop.DBus.Properties", "Get",
+                    "xyz.openbmc_project.Control.Power.Cap", "PowerCapEnable");
+            };
+        getValidChassisPath(asyncResp, std::move(getChassisPath));
+    }
     void doGet(crow::Response& res, const crow::Request& req,
                const std::vector<std::string>& params) override
     {
@@ -227,7 +330,38 @@
     void doPatch(crow::Response& res, const crow::Request& req,
                  const std::vector<std::string>& params) override
     {
-        setSensorOverride(res, req, params, typeList, "Power");
+        if (params.size() != 1)
+        {
+            messages::internalError(res);
+            res.end();
+            return;
+        }
+
+        const std::string& chassisName = params[0];
+        auto asyncResp = std::make_shared<SensorsAsyncResp>(res, chassisName,
+                                                            typeList, "Power");
+
+        std::optional<std::vector<nlohmann::json>> voltageCollections;
+        std::optional<std::vector<nlohmann::json>> powerCtlCollections;
+
+        if (!json_util::readJson(req, asyncResp->res, "PowerControl",
+                                 powerCtlCollections, "Voltages",
+                                 voltageCollections))
+        {
+            return;
+        }
+
+        if (powerCtlCollections)
+        {
+            setPowerCapOverride(asyncResp, *powerCtlCollections);
+        }
+        if (voltageCollections)
+        {
+            std::unordered_map<std::string, std::vector<nlohmann::json>>
+                allCollections;
+            allCollections.emplace("Voltages", *std::move(voltageCollections));
+            setSensorOverride(asyncResp, allCollections, chassisName, typeList);
+        }
     }
 };