control: Add set target on missing owner action

Add the YAML based set_speed_on_missing_owner action function as an
action class for JSON configs to use.

Change-Id: Iebe0cc7bbaf063ef33ca0b918dd1d8576e2425eb
Signed-off-by: Matthew Barth <msbarth@us.ibm.com>
diff --git a/control/Makefile.am b/control/Makefile.am
index c1df84b..7d7824a 100644
--- a/control/Makefile.am
+++ b/control/Makefile.am
@@ -32,7 +32,8 @@
 	json/group.cpp \
 	json/event.cpp \
 	json/actions/default_floor.cpp \
-	json/actions/request_target_base.cpp
+	json/actions/request_target_base.cpp \
+	json/actions/missing_owner_target.cpp
 else
 phosphor_fan_control_SOURCES += \
 	argument.cpp \
diff --git a/control/json/actions/default_floor.cpp b/control/json/actions/default_floor.cpp
index 1f95ffa..81bbd41 100644
--- a/control/json/actions/default_floor.cpp
+++ b/control/json/actions/default_floor.cpp
@@ -22,7 +22,6 @@
 #include <nlohmann/json.hpp>
 
 #include <algorithm>
-#include <tuple>
 
 namespace phosphor::fan::control::json
 {
diff --git a/control/json/actions/missing_owner_target.cpp b/control/json/actions/missing_owner_target.cpp
new file mode 100644
index 0000000..3cbd8a1
--- /dev/null
+++ b/control/json/actions/missing_owner_target.cpp
@@ -0,0 +1,72 @@
+/**
+ * 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 "missing_owner_target.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;
+
+MissingOwnerTarget::MissingOwnerTarget(const json& jsonObj) :
+    ActionBase(MissingOwnerTarget::name)
+{
+    setTarget(jsonObj);
+}
+
+void MissingOwnerTarget::run(Zone& zone, const Group& group)
+{
+    const auto& members = group.getMembers();
+    auto isMissingOwner =
+        std::any_of(members.begin(), members.end(),
+                    [&intf = group.getInterface()](const auto& member) {
+                        return !Manager::hasOwner(member, intf);
+                    });
+    if (isMissingOwner)
+    {
+        zone.setTarget(_target);
+    }
+    // Update group's fan control active allowed based on action results
+    zone.setActiveAllow(group.getName(), !isMissingOwner);
+}
+
+void MissingOwnerTarget::setTarget(const json& jsonObj)
+{
+    if (!jsonObj.contains("speed"))
+    {
+        log<level::ERR>(
+            fmt::format("Action {}: Missing required speed value", getName())
+                .c_str(),
+            entry("JSON=%s", jsonObj.dump().c_str()));
+        throw std::runtime_error(
+            fmt::format("Action {}: Missing required speed value", getName())
+                .c_str());
+    }
+    _target = jsonObj["speed"].get<uint64_t>();
+}
+
+} // namespace phosphor::fan::control::json
diff --git a/control/json/actions/missing_owner_target.hpp b/control/json/actions/missing_owner_target.hpp
new file mode 100644
index 0000000..35fb5a9
--- /dev/null
+++ b/control/json/actions/missing_owner_target.hpp
@@ -0,0 +1,84 @@
+/**
+ * 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 MissingOwnerTarget - Action to set a target when an owner is missing
+ *
+ * Sets the fans to a configured target when any service owner associated to the
+ * group is missing. Once all services are functional and providing all the
+ * group data again, active fan target changes are allowed.
+ */
+class MissingOwnerTarget :
+    public ActionBase,
+    public ActionRegister<MissingOwnerTarget>
+{
+  public:
+    /* Name of this action */
+    static constexpr auto name = "set_speed_on_missing_owner";
+
+    MissingOwnerTarget() = delete;
+    MissingOwnerTarget(const MissingOwnerTarget&) = delete;
+    MissingOwnerTarget(MissingOwnerTarget&&) = delete;
+    MissingOwnerTarget& operator=(const MissingOwnerTarget&) = delete;
+    MissingOwnerTarget& operator=(MissingOwnerTarget&&) = delete;
+    ~MissingOwnerTarget() = default;
+
+    /**
+     * @brief Set target on an owner missing
+     *
+     * @param[in] jsonObj - JSON containing the configured target to use
+     */
+    explicit MissingOwnerTarget(const json& jsonObj);
+
+    /**
+     * @brief Run the action
+     *
+     * Updates the services of the group, then determines if any of the
+     * services hosting the members of the group are not owned on dbus
+     * resulting in the zone's target being set/held at the configured target.
+     *
+     * @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;
+
+  private:
+    /* Target for this action */
+    uint64_t _target;
+
+    /**
+     * @brief Parse and set the target
+     *
+     * @param[in] jsonObj - JSON object for the action
+     *
+     * Sets the target to use when running the action
+     */
+    void setTarget(const json& jsonObj);
+};
+
+} // namespace phosphor::fan::control::json