control: Add request target base action

Add the YAML based set_request_speed_base_with_max action function as an
action class for JSON configs to use. This action required to be
enhanced to handle the different supported data types of groups that
could be configured in the JSON with this action.

Change-Id: I8cc223cc10d33462ddc303145fee08347ea7c8b5
Signed-off-by: Matthew Barth <msbarth@us.ibm.com>
diff --git a/control/json/actions/request_target_base.cpp b/control/json/actions/request_target_base.cpp
new file mode 100644
index 0000000..2de3e46
--- /dev/null
+++ b/control/json/actions/request_target_base.cpp
@@ -0,0 +1,92 @@
+/**
+ * Copyright © 2021 IBM Corporation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+#include "request_target_base.hpp"
+
+#include "../manager.hpp"
+#include "../zone.hpp"
+#include "group.hpp"
+
+#include <fmt/format.h>
+
+#include <nlohmann/json.hpp>
+#include <phosphor-logging/log.hpp>
+
+#include <algorithm>
+
+namespace phosphor::fan::control::json
+{
+
+using json = nlohmann::json;
+using namespace phosphor::logging;
+
+RequestTargetBase::RequestTargetBase(const json&) :
+    ActionBase(RequestTargetBase::name)
+{
+    // There are no JSON configuration parameters for this action
+}
+
+void RequestTargetBase::run(Zone& zone, const Group& group)
+{
+    uint64_t base = 0;
+    for (const auto& member : group.getMembers())
+    {
+        try
+        {
+            auto value = Manager::getObjValueVariant(
+                member, group.getInterface(), group.getProperty());
+            if (auto intPtr = std::get_if<int64_t>(&value))
+            {
+                // Throw out any negative values as those are not valid
+                // to use as a fan target base
+                if (*intPtr < 0)
+                {
+                    continue;
+                }
+                base = std::max(base, static_cast<uint64_t>(*intPtr));
+            }
+            else if (auto dblPtr = std::get_if<double>(&value))
+            {
+                // Throw out any negative values as those are not valid
+                // to use as a fan target base
+                if (*dblPtr < 0)
+                {
+                    continue;
+                }
+                // Precision of a double not a concern with fan targets
+                base = std::max(base, static_cast<uint64_t>(*dblPtr));
+            }
+            else
+            {
+                // Unsupported group member type for this action
+                log<level::ERR>(
+                    fmt::format("Action {}: Unsupported group member type "
+                                "given. [object = {} : {} : {}]",
+                                getName(), member, group.getInterface(),
+                                group.getProperty())
+                        .c_str());
+            }
+        }
+        catch (const std::out_of_range& oore)
+        {
+            // Property value not found, base request target unchanged
+        }
+    }
+
+    // A request target base of 0 defaults to the current target
+    zone.setRequestTargetBase(base);
+}
+
+} // namespace phosphor::fan::control::json
diff --git a/control/json/actions/request_target_base.hpp b/control/json/actions/request_target_base.hpp
new file mode 100644
index 0000000..6285dc5
--- /dev/null
+++ b/control/json/actions/request_target_base.hpp
@@ -0,0 +1,75 @@
+/**
+ * Copyright © 2021 IBM Corporation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+#pragma once
+
+#include "../zone.hpp"
+#include "action.hpp"
+#include "group.hpp"
+
+#include <nlohmann/json.hpp>
+
+namespace phosphor::fan::control::json
+{
+
+using json = nlohmann::json;
+
+/**
+ * @class RequestTargetBase - Action to set the requested target base
+ *
+ * Sets the base of a calculated requested target to the maximum value found
+ * from the properties given within a group. The requested target base is what
+ * the calculated requested target should be determined from when changing fan
+ * targets. By default, the base of the next calculated requested target is the
+ * current target of the zone. This action allows that base to be changed
+ * according to the maximum value found from a given group of dbus objects.
+ */
+class RequestTargetBase :
+    public ActionBase,
+    public ActionRegister<RequestTargetBase>
+{
+  public:
+    /* Name of this action */
+    static constexpr auto name = "set_request_speed_base_with_max";
+
+    RequestTargetBase() = delete;
+    RequestTargetBase(const RequestTargetBase&) = delete;
+    RequestTargetBase(RequestTargetBase&&) = delete;
+    RequestTargetBase& operator=(const RequestTargetBase&) = delete;
+    RequestTargetBase& operator=(RequestTargetBase&&) = delete;
+    ~RequestTargetBase() = default;
+
+    /**
+     * @brief Update the requested target base
+     *
+     * No JSON configuration parameters required
+     */
+    explicit RequestTargetBase(const json&);
+
+    /**
+     * @brief Run the action
+     *
+     * Determines the maximum value from the properties of the group of dbus
+     * objects and sets the requested target base to this value. Only positive
+     * integer or floating point types are supported as these are the only
+     * valid types for a fan target to be based off of.
+     *
+     * @param[in] zone - Zone to run the action on
+     * @param[in] group - Group of dbus objects the action runs against
+     */
+    void run(Zone& zone, const Group& group) override;
+};
+
+} // namespace phosphor::fan::control::json