control: Store/use trigger enable functions on events

Create and store trigger enablement functions on the events based on the
trigger's configuration so that they can be enabled separately from when
the event is created. This will allow actions that load events to be
able to be parsed and then enabled when the action determines that the
events should be enabled. It also supports the use of SIGHUP to reload
the event configuration since the events JSON configuration must
successfully be loaded before the newly created events can be enabled in
place of the currently enabled events.

Change-Id: I31871ee1691d5e6b26fe16cde2a829c426ad6504
Signed-off-by: Matthew Barth <msbarth@us.ibm.com>
diff --git a/control/json/triggers/init.cpp b/control/json/triggers/init.cpp
index 160b725..d21a918 100644
--- a/control/json/triggers/init.cpp
+++ b/control/json/triggers/init.cpp
@@ -19,6 +19,7 @@
 #include "action.hpp"
 #include "group.hpp"
 #include "sdbusplus.hpp"
+#include "trigger_aliases.hpp"
 
 #include <fmt/format.h>
 
@@ -119,9 +120,9 @@
     }
 }
 
-void triggerInit(const json& jsonObj, const std::string& eventName,
-                 Manager* mgr,
-                 std::vector<std::unique_ptr<ActionBase>>& actions)
+enableTrigger triggerInit(const json& jsonObj, const std::string& eventName,
+                          Manager* mgr,
+                          std::vector<std::unique_ptr<ActionBase>>& actions)
 {
     // Get the method handler if configured
     auto handler = methods.end();
@@ -137,29 +138,37 @@
         // Groups are optional, so a method is only required if there are groups
         // i.e.) An init triggered event without any groups results in just
         // running the actions
-        for (const auto& group : action->getGroups())
+        if (!action->getGroups().empty() && handler == methods.end())
         {
-            if (handler == methods.end())
-            {
-                // Construct list of available methods
-                auto availMethods = std::accumulate(
-                    std::next(methods.begin()), methods.end(),
-                    methods.begin()->first, [](auto list, auto method) {
-                        return std::move(list) + ", " + method.first;
-                    });
-                auto msg =
-                    fmt::format("Event '{}' requires a supported method given "
-                                "to be init driven, available methods: {}",
-                                eventName, availMethods);
-                log<level::ERR>(msg.c_str());
-                throw std::runtime_error(msg.c_str());
-            }
-            // Call method handler for each group in the actions
-            handler->second(mgr, group);
+            // Construct list of available methods
+            auto availMethods = std::accumulate(
+                std::next(methods.begin()), methods.end(),
+                methods.begin()->first, [](auto list, auto method) {
+                    return std::move(list) + ", " + method.first;
+                });
+            auto msg =
+                fmt::format("Event '{}' requires a supported method given to "
+                            "be init driven, available methods: {}",
+                            eventName, availMethods);
+            log<level::ERR>(msg.c_str());
+            throw std::runtime_error(msg.c_str());
         }
-        // Run the action
-        action->run();
     }
+
+    return [handler = std::move(handler)](
+               const std::string& eventName, Manager* mgr,
+               std::vector<std::unique_ptr<ActionBase>>& actions) {
+        for (auto& action : actions)
+        {
+            for (const auto& group : action->getGroups())
+            {
+                // Call method handler for each group in the actions
+                handler->second(mgr, group);
+            }
+            // Run the action
+            action->run();
+        }
+    };
 }
 
 } // namespace phosphor::fan::control::json::trigger::init
diff --git a/control/json/triggers/init.hpp b/control/json/triggers/init.hpp
index 6447435..dffee1f 100644
--- a/control/json/triggers/init.hpp
+++ b/control/json/triggers/init.hpp
@@ -18,6 +18,7 @@
 #include "../manager.hpp"
 #include "action.hpp"
 #include "group.hpp"
+#include "trigger_aliases.hpp"
 
 #include <nlohmann/json.hpp>
 
@@ -70,8 +71,8 @@
  * an event so the initial data for an event is collected, processed, and run
  * before any signal may be received.
  */
-void triggerInit(const json& jsonObj, const std::string& eventName,
-                 Manager* mgr,
-                 std::vector<std::unique_ptr<ActionBase>>& actions);
+enableTrigger triggerInit(const json& jsonObj, const std::string& eventName,
+                          Manager* mgr,
+                          std::vector<std::unique_ptr<ActionBase>>& actions);
 
 } // namespace phosphor::fan::control::json::trigger::init
diff --git a/control/json/triggers/signal.cpp b/control/json/triggers/signal.cpp
index 9e793f0..4869ee9 100644
--- a/control/json/triggers/signal.cpp
+++ b/control/json/triggers/signal.cpp
@@ -19,6 +19,7 @@
 #include "action.hpp"
 #include "group.hpp"
 #include "handlers.hpp"
+#include "trigger_aliases.hpp"
 
 #include <fmt/format.h>
 
@@ -214,9 +215,9 @@
     }
 }
 
-void triggerSignal(const json& jsonObj, const std::string& eventName,
-                   Manager* mgr,
-                   std::vector<std::unique_ptr<ActionBase>>& actions)
+enableTrigger triggerSignal(const json& jsonObj, const std::string& eventName,
+                            Manager* mgr,
+                            std::vector<std::unique_ptr<ActionBase>>& actions)
 {
     auto subscriber = signals.end();
     if (jsonObj.contains("signal"))
@@ -241,11 +242,15 @@
         throw std::runtime_error(msg.c_str());
     }
 
-    for (auto& action : actions)
-    {
-        // Call signal subscriber for each group in the action
-        subscriber->second(mgr, eventName, action);
-    }
+    return [subscriber = std::move(subscriber)](
+               const std::string& eventName, Manager* mgr,
+               std::vector<std::unique_ptr<ActionBase>>& actions) {
+        for (auto& action : actions)
+        {
+            // Call signal subscriber for each group in the action
+            subscriber->second(mgr, eventName, action);
+        }
+    };
 }
 
 } // namespace phosphor::fan::control::json::trigger::signal
diff --git a/control/json/triggers/signal.hpp b/control/json/triggers/signal.hpp
index 61b03e3..a1d8109 100644
--- a/control/json/triggers/signal.hpp
+++ b/control/json/triggers/signal.hpp
@@ -17,6 +17,7 @@
 
 #include "../manager.hpp"
 #include "action.hpp"
+#include "trigger_aliases.hpp"
 
 #include <nlohmann/json.hpp>
 #include <sdbusplus/message.hpp>
@@ -104,8 +105,8 @@
  * subscribed to run its corresponding actions when a signal, per its
  * configuration, is received.
  */
-void triggerSignal(const json& jsonObj, const std::string& eventName,
-                   Manager* mgr,
-                   std::vector<std::unique_ptr<ActionBase>>& actions);
+enableTrigger triggerSignal(const json& jsonObj, const std::string& eventName,
+                            Manager* mgr,
+                            std::vector<std::unique_ptr<ActionBase>>& actions);
 
 } // namespace phosphor::fan::control::json::trigger::signal
diff --git a/control/json/triggers/timer.cpp b/control/json/triggers/timer.cpp
index 641e33b..fcb5e95 100644
--- a/control/json/triggers/timer.cpp
+++ b/control/json/triggers/timer.cpp
@@ -16,6 +16,7 @@
 #include "timer.hpp"
 
 #include "../manager.hpp"
+#include "trigger_aliases.hpp"
 
 #include <fmt/format.h>
 
@@ -69,17 +70,20 @@
         jsonObj["interval"].get<uint64_t>());
 }
 
-void triggerTimer(const json& jsonObj, const std::string& eventName,
-                  Manager* mgr,
-                  std::vector<std::unique_ptr<ActionBase>>& actions)
+enableTrigger triggerTimer(const json& jsonObj, const std::string& eventName,
+                           Manager* mgr,
+                           std::vector<std::unique_ptr<ActionBase>>& actions)
 {
     // Get the type and interval of this timer from the JSON
     auto type = getType(jsonObj);
     auto interval = getInterval(jsonObj);
 
-    // Create/add event timer
-    auto tpPtr = std::make_unique<TimerPkg>(eventName, std::ref(actions));
-    mgr->addTimer(type, interval, std::move(tpPtr));
+    return [type = std::move(type), interval = std::move(interval)](
+               const std::string& eventName, Manager* mgr,
+               std::vector<std::unique_ptr<ActionBase>>& actions) {
+        auto tpPtr = std::make_unique<TimerPkg>(eventName, std::ref(actions));
+        mgr->addTimer(type, interval, std::move(tpPtr));
+    };
 }
 
 } // namespace phosphor::fan::control::json::trigger::timer
diff --git a/control/json/triggers/timer.hpp b/control/json/triggers/timer.hpp
index 1be7fdd..1e6a931 100644
--- a/control/json/triggers/timer.hpp
+++ b/control/json/triggers/timer.hpp
@@ -17,6 +17,7 @@
 
 #include "../manager.hpp"
 #include "action.hpp"
+#include "trigger_aliases.hpp"
 
 #include <nlohmann/json.hpp>
 
@@ -57,8 +58,8 @@
  * have their timers started. Once the timer expires, per its configuration,
  * the corresponding event's actions are run.
  */
-void triggerTimer(const json& jsonObj, const std::string& eventName,
-                  Manager* mgr,
-                  std::vector<std::unique_ptr<ActionBase>>& actions);
+enableTrigger triggerTimer(const json& jsonObj, const std::string& eventName,
+                           Manager* mgr,
+                           std::vector<std::unique_ptr<ActionBase>>& actions);
 
 } // namespace phosphor::fan::control::json::trigger::timer
diff --git a/control/json/triggers/trigger.hpp b/control/json/triggers/trigger.hpp
index 4e7e213..ced50d7 100644
--- a/control/json/triggers/trigger.hpp
+++ b/control/json/triggers/trigger.hpp
@@ -15,29 +15,16 @@
  */
 #pragma once
 
-#include "action.hpp"
 #include "init.hpp"
-#include "manager.hpp"
 #include "signal.hpp"
 #include "timer.hpp"
+#include "trigger_aliases.hpp"
 
-#include <nlohmann/json.hpp>
-
-#include <functional>
 #include <map>
-#include <memory>
-#include <vector>
 
 namespace phosphor::fan::control::json::trigger
 {
 
-using json = nlohmann::json;
-
-// Trigger creation function
-using createTrigger =
-    std::function<void(const json&, const std::string&, Manager*,
-                       std::vector<std::unique_ptr<ActionBase>>&)>;
-
 // Mapping of trigger class name to its creation function
 static const std::map<std::string, createTrigger> triggers = {
     {"timer", timer::triggerTimer},
diff --git a/control/json/triggers/trigger_aliases.hpp b/control/json/triggers/trigger_aliases.hpp
new file mode 100644
index 0000000..c390864
--- /dev/null
+++ b/control/json/triggers/trigger_aliases.hpp
@@ -0,0 +1,41 @@
+/**
+ * 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 "action.hpp"
+#include "manager.hpp"
+
+#include <nlohmann/json.hpp>
+
+#include <functional>
+#include <memory>
+#include <vector>
+
+namespace phosphor::fan::control::json::trigger
+{
+
+using json = nlohmann::json;
+
+// Trigger enablement function
+using enableTrigger = std::function<void(
+    const std::string&, Manager*, std::vector<std::unique_ptr<ActionBase>>&)>;
+
+// Trigger creation function
+using createTrigger =
+    std::function<enableTrigger(const json&, const std::string&, Manager*,
+                                std::vector<std::unique_ptr<ActionBase>>&)>;
+
+} // namespace phosphor::fan::control::json::trigger