Add TriggerManager and Trigger objects

Implemented initial version of Trigger and TriggerManager DBus
interfaces. Now DBus user is able to add new Trigger and delete it.
There is no support for other functionality added.

Tested:
 - Built using yocto dependencies with success
 - Unit tests passed
 - Verified manually if Trigger and TriggerManager works as
   expected

Change-Id: Ie68463526ecccc3be67cc7bfaaf9a9aa7326dee6
Signed-off-by: Wludzik, Jozef <jozef.wludzik@intel.com>
diff --git a/src/interfaces/trigger.hpp b/src/interfaces/trigger.hpp
new file mode 100644
index 0000000..328e581
--- /dev/null
+++ b/src/interfaces/trigger.hpp
@@ -0,0 +1,17 @@
+#pragma once
+
+#include <string>
+
+namespace interfaces
+{
+
+class Trigger
+{
+  public:
+    virtual ~Trigger() = default;
+
+    virtual std::string getName() const = 0;
+    virtual std::string getPath() const = 0;
+};
+
+} // namespace interfaces
diff --git a/src/interfaces/trigger_factory.hpp b/src/interfaces/trigger_factory.hpp
new file mode 100644
index 0000000..91f7f4b
--- /dev/null
+++ b/src/interfaces/trigger_factory.hpp
@@ -0,0 +1,28 @@
+#pragma once
+
+#include "interfaces/trigger.hpp"
+#include "interfaces/trigger_manager.hpp"
+#include "interfaces/trigger_types.hpp"
+
+#include <memory>
+#include <utility>
+
+namespace interfaces
+{
+
+class TriggerFactory
+{
+  public:
+    virtual ~TriggerFactory() = default;
+
+    virtual std::unique_ptr<interfaces::Trigger> make(
+        const std::string& name, bool isDiscrete, bool logToJournal,
+        bool logToRedfish, bool updateReport,
+        const std::vector<
+            std::pair<sdbusplus::message::object_path, std::string>>& sensors,
+        const std::vector<std::string>& reportNames,
+        const TriggerThresholdParams& thresholdParams,
+        interfaces::TriggerManager& triggerManager) const = 0;
+};
+
+} // namespace interfaces
diff --git a/src/interfaces/trigger_manager.hpp b/src/interfaces/trigger_manager.hpp
new file mode 100644
index 0000000..878fd57
--- /dev/null
+++ b/src/interfaces/trigger_manager.hpp
@@ -0,0 +1,16 @@
+#pragma once
+
+#include "interfaces/trigger.hpp"
+
+namespace interfaces
+{
+
+class TriggerManager
+{
+  public:
+    virtual ~TriggerManager() = default;
+
+    virtual void removeTrigger(const Trigger* trigger) = 0;
+};
+
+} // namespace interfaces
diff --git a/src/interfaces/trigger_types.hpp b/src/interfaces/trigger_types.hpp
new file mode 100644
index 0000000..ef61aa6
--- /dev/null
+++ b/src/interfaces/trigger_types.hpp
@@ -0,0 +1,48 @@
+#pragma once
+
+#include <sdbusplus/message/types.hpp>
+
+#include <string>
+#include <tuple>
+#include <utility>
+#include <vector>
+
+namespace discrete
+{
+
+enum class Severity
+{
+    ok = 0,
+    warning,
+    critical
+};
+
+using ThresholdParam = std::tuple<std::string, std::underlying_type_t<Severity>,
+                                  std::variant<double>, uint64_t>;
+} // namespace discrete
+
+namespace numeric
+{
+
+enum class Type
+{
+    lowerCritical = 0,
+    lowerWarning,
+    upperWarning,
+    upperCritical
+};
+
+enum class Direction
+{
+    either = 0,
+    decreasing,
+    increasing
+};
+
+using ThresholdParam = std::tuple<std::underlying_type_t<Type>, uint64_t,
+                                  std::underlying_type_t<Direction>, double>;
+} // namespace numeric
+
+using TriggerThresholdParams =
+    std::variant<std::vector<numeric::ThresholdParam>,
+                 std::vector<discrete::ThresholdParam>>;
diff --git a/src/report_manager.hpp b/src/report_manager.hpp
index 2180d52..a1a9007 100644
--- a/src/report_manager.hpp
+++ b/src/report_manager.hpp
@@ -27,7 +27,6 @@
     ReportManager& operator=(ReportManager&&) = delete;
 
     void removeReport(const interfaces::Report* report) override;
-    static bool verifyScanPeriod(const uint64_t scanPeriod);
 
   private:
     std::unique_ptr<interfaces::ReportFactory> reportFactory;
diff --git a/src/telemetry.hpp b/src/telemetry.hpp
index 4cc138e..20749e4 100644
--- a/src/telemetry.hpp
+++ b/src/telemetry.hpp
@@ -4,6 +4,8 @@
 #include "report_factory.hpp"
 #include "report_manager.hpp"
 #include "sensor_cache.hpp"
+#include "trigger_factory.hpp"
+#include "trigger_manager.hpp"
 
 #include <sdbusplus/asio/connection.hpp>
 #include <sdbusplus/asio/object_server.hpp>
@@ -19,10 +21,13 @@
                       std::make_unique<PersistentJsonStorage>(
                           interfaces::JsonStorage::DirectoryPath(
                               "/var/lib/telemetry/Reports")),
-                      objServer)
+                      objServer),
+        triggerManager(std::make_unique<TriggerFactory>(bus, objServer),
+                       objServer)
     {}
 
   private:
     std::shared_ptr<sdbusplus::asio::object_server> objServer;
     ReportManager reportManager;
+    TriggerManager triggerManager;
 };
diff --git a/src/trigger.cpp b/src/trigger.cpp
new file mode 100644
index 0000000..4152dd1
--- /dev/null
+++ b/src/trigger.cpp
@@ -0,0 +1,60 @@
+#include "trigger.hpp"
+
+#include "interfaces/types.hpp"
+
+Trigger::Trigger(
+    boost::asio::io_context& ioc,
+    const std::shared_ptr<sdbusplus::asio::object_server>& objServer,
+    const std::string& nameIn, const bool isDiscrete, const bool logToJournal,
+    const bool logToRedfish, const bool updateReport,
+    const std::vector<std::pair<sdbusplus::message::object_path, std::string>>&
+        sensorsIn,
+    const std::vector<std::string>& reportNamesIn,
+    const TriggerThresholdParams& thresholdParamsIn,
+    interfaces::TriggerManager& triggerManager) :
+    name(nameIn),
+    path(triggerDir + name), persistent(false), sensors(sensorsIn),
+    reportNames(reportNamesIn), thresholdParams(thresholdParamsIn)
+{
+    deleteIface = objServer->add_unique_interface(
+        path, deleteIfaceName, [this, &ioc, &triggerManager](auto& dbusIface) {
+            dbusIface.register_method("Delete", [this, &ioc, &triggerManager] {
+                boost::asio::post(ioc, [this, &triggerManager] {
+                    triggerManager.removeTrigger(this);
+                });
+            });
+        });
+
+    triggerIface = objServer->add_unique_interface(
+        path, triggerIfaceName,
+        [this, isDiscrete, logToJournal, logToRedfish,
+         updateReport](auto& dbusIface) {
+            dbusIface.register_property_r(
+                "Persistent", persistent,
+                sdbusplus::vtable::property_::emits_change,
+                [](const auto& x) { return x; });
+            dbusIface.register_property_r(
+                "Thresholds", thresholdParams,
+                sdbusplus::vtable::property_::emits_change,
+                [](const auto& x) { return x; });
+            dbusIface.register_property_r(
+                "Sensors", sensors, sdbusplus::vtable::property_::emits_change,
+                [](const auto& x) { return x; });
+            dbusIface.register_property_r(
+                "ReportNames", reportNames,
+                sdbusplus::vtable::property_::emits_change,
+                [](const auto& x) { return x; });
+            dbusIface.register_property_r("Discrete", isDiscrete,
+                                          sdbusplus::vtable::property_::const_,
+                                          [](const auto& x) { return x; });
+            dbusIface.register_property_r("LogToJournal", logToJournal,
+                                          sdbusplus::vtable::property_::const_,
+                                          [](const auto& x) { return x; });
+            dbusIface.register_property_r("LogToRedfish", logToRedfish,
+                                          sdbusplus::vtable::property_::const_,
+                                          [](const auto& x) { return x; });
+            dbusIface.register_property_r("UpdateReport", updateReport,
+                                          sdbusplus::vtable::property_::const_,
+                                          [](const auto& x) { return x; });
+        });
+}
diff --git a/src/trigger.hpp b/src/trigger.hpp
new file mode 100644
index 0000000..5092b6d
--- /dev/null
+++ b/src/trigger.hpp
@@ -0,0 +1,60 @@
+#pragma once
+
+#include "interfaces/trigger.hpp"
+#include "interfaces/trigger_manager.hpp"
+#include "interfaces/trigger_types.hpp"
+
+#include <boost/asio/io_context.hpp>
+#include <sdbusplus/asio/object_server.hpp>
+
+#include <memory>
+
+class Trigger : public interfaces::Trigger
+{
+  public:
+    Trigger(
+        boost::asio::io_context& ioc,
+        const std::shared_ptr<sdbusplus::asio::object_server>& objServer,
+        const std::string& name, const bool isDiscrete, const bool logToJournal,
+        const bool logToRedfish, const bool updateReport,
+        const std::vector<
+            std::pair<sdbusplus::message::object_path, std::string>>& sensorsIn,
+        const std::vector<std::string>& reportNames,
+        const TriggerThresholdParams& thresholds,
+        interfaces::TriggerManager& triggerManager);
+
+    Trigger(const Trigger&) = delete;
+    Trigger(Trigger&&) = delete;
+    Trigger& operator=(const Trigger&) = delete;
+    Trigger& operator=(Trigger&&) = delete;
+
+    std::string getName() const override
+    {
+        return name;
+    }
+
+    std::string getPath() const override
+    {
+        return path;
+    }
+
+  private:
+    const std::string name;
+    const std::string path;
+    bool persistent;
+    std::vector<std::pair<sdbusplus::message::object_path, std::string>>
+        sensors;
+    std::vector<std::string> reportNames;
+    TriggerThresholdParams thresholdParams;
+
+    std::unique_ptr<sdbusplus::asio::dbus_interface> deleteIface;
+    std::unique_ptr<sdbusplus::asio::dbus_interface> triggerIface;
+
+  public:
+    static constexpr const char* triggerIfaceName =
+        "xyz.openbmc_project.Telemetry.Trigger";
+    static constexpr const char* triggerDir =
+        "/xyz/openbmc_project/Telemetry/Triggers/";
+    static constexpr const char* deleteIfaceName =
+        "xyz.openbmc_project.Object.Delete";
+};
diff --git a/src/trigger_factory.cpp b/src/trigger_factory.cpp
new file mode 100644
index 0000000..0a45dec
--- /dev/null
+++ b/src/trigger_factory.cpp
@@ -0,0 +1,25 @@
+#include "trigger_factory.hpp"
+
+#include "trigger.hpp"
+
+TriggerFactory::TriggerFactory(
+    std::shared_ptr<sdbusplus::asio::connection> bus,
+    std::shared_ptr<sdbusplus::asio::object_server> objServer) :
+    bus(std::move(bus)),
+    objServer(std::move(objServer))
+{}
+
+std::unique_ptr<interfaces::Trigger> TriggerFactory::make(
+    const std::string& name, bool isDiscrete, bool logToJournal,
+    bool logToRedfish, bool updateReport,
+    const std::vector<std::pair<sdbusplus::message::object_path, std::string>>&
+        sensors,
+    const std::vector<std::string>& reportNames,
+    const TriggerThresholdParams& thresholdParams,
+    interfaces::TriggerManager& reportManager) const
+{
+    return std::make_unique<Trigger>(bus->get_io_context(), objServer, name,
+                                     isDiscrete, logToJournal, logToRedfish,
+                                     updateReport, sensors, reportNames,
+                                     thresholdParams, reportManager);
+}
diff --git a/src/trigger_factory.hpp b/src/trigger_factory.hpp
new file mode 100644
index 0000000..60a9fb4
--- /dev/null
+++ b/src/trigger_factory.hpp
@@ -0,0 +1,25 @@
+#pragma once
+
+#include "interfaces/trigger_factory.hpp"
+
+#include <sdbusplus/asio/object_server.hpp>
+
+class TriggerFactory : public interfaces::TriggerFactory
+{
+  public:
+    TriggerFactory(std::shared_ptr<sdbusplus::asio::connection> bus,
+                   std::shared_ptr<sdbusplus::asio::object_server> objServer);
+
+    std::unique_ptr<interfaces::Trigger> make(
+        const std::string& name, bool isDiscrete, bool logToJournal,
+        bool logToRedfish, bool updateReport,
+        const std::vector<
+            std::pair<sdbusplus::message::object_path, std::string>>& sensors,
+        const std::vector<std::string>& reportNames,
+        const TriggerThresholdParams& thresholdParams,
+        interfaces::TriggerManager& triggerManager) const override;
+
+  private:
+    std::shared_ptr<sdbusplus::asio::connection> bus;
+    std::shared_ptr<sdbusplus::asio::object_server> objServer;
+};
diff --git a/src/trigger_manager.cpp b/src/trigger_manager.cpp
new file mode 100644
index 0000000..cc164e4
--- /dev/null
+++ b/src/trigger_manager.cpp
@@ -0,0 +1,50 @@
+#include "trigger_manager.hpp"
+
+TriggerManager::TriggerManager(
+    std::unique_ptr<interfaces::TriggerFactory> triggerFactoryIn,
+    const std::shared_ptr<sdbusplus::asio::object_server>& objServer) :
+    triggerFactory(std::move(triggerFactoryIn))
+{
+    managerIface = objServer->add_unique_interface(
+        triggerManagerPath, triggerManagerIfaceName, [this](auto& iface) {
+            iface.register_method(
+                "AddTrigger",
+                [this](
+                    const std::string& name, bool isDiscrete, bool logToJournal,
+                    bool logToRedfish, bool updateReport,
+                    const std::vector<std::pair<sdbusplus::message::object_path,
+                                                std::string>>& sensors,
+                    const std::vector<std::string>& reportNames,
+                    const TriggerThresholdParams& thresholds) {
+                    if (triggers.size() >= maxTriggers)
+                    {
+                        throw sdbusplus::exception::SdBusError(
+                            static_cast<int>(std::errc::too_many_files_open),
+                            "Reached maximal trigger count");
+                    }
+
+                    for (const auto& trigger : triggers)
+                    {
+                        if (trigger->getName() == name)
+                        {
+                            throw sdbusplus::exception::SdBusError(
+                                static_cast<int>(std::errc::file_exists),
+                                "Duplicate trigger");
+                        }
+                    }
+
+                    triggers.emplace_back(triggerFactory->make(
+                        name, isDiscrete, logToJournal, logToRedfish,
+                        updateReport, sensors, reportNames, thresholds, *this));
+                    return triggers.back()->getPath();
+                });
+        });
+}
+
+void TriggerManager::removeTrigger(const interfaces::Trigger* trigger)
+{
+    triggers.erase(
+        std::remove_if(triggers.begin(), triggers.end(),
+                       [trigger](const auto& x) { return trigger == x.get(); }),
+        triggers.end());
+}
diff --git a/src/trigger_manager.hpp b/src/trigger_manager.hpp
new file mode 100644
index 0000000..b5b4a22
--- /dev/null
+++ b/src/trigger_manager.hpp
@@ -0,0 +1,36 @@
+#pragma once
+
+#include "interfaces/trigger_factory.hpp"
+#include "interfaces/trigger_manager.hpp"
+
+#include <sdbusplus/asio/object_server.hpp>
+
+#include <memory>
+#include <vector>
+
+class TriggerManager : public interfaces::TriggerManager
+{
+  public:
+    TriggerManager(
+        std::unique_ptr<interfaces::TriggerFactory> triggerFactory,
+        const std::shared_ptr<sdbusplus::asio::object_server>& objServer);
+
+    TriggerManager(TriggerManager&) = delete;
+    TriggerManager(TriggerManager&&) = delete;
+    TriggerManager& operator=(TriggerManager&) = delete;
+    TriggerManager& operator=(TriggerManager&&) = delete;
+
+    void removeTrigger(const interfaces::Trigger* trigger) override;
+
+  private:
+    std::unique_ptr<interfaces::TriggerFactory> triggerFactory;
+    std::unique_ptr<sdbusplus::asio::dbus_interface> managerIface;
+    std::vector<std::unique_ptr<interfaces::Trigger>> triggers;
+
+  public:
+    static constexpr size_t maxTriggers{TELEMETRY_MAX_TRIGGERS};
+    static constexpr const char* triggerManagerIfaceName =
+        "xyz.openbmc_project.Telemetry.TriggerManager";
+    static constexpr const char* triggerManagerPath =
+        "/xyz/openbmc_project/Telemetry/Triggers";
+};