Report: Add TriggerIds property

This change is adding TriggerIds property for Report interface. It is an
array of strings, each representing id of trigger which may update given
report Readings property, due to UpdateReport action. This properly is
read-only, but it can be changed in the runtime, when:
- New Trigger is made using AddTrigger dbus method, when ReportId
  argument contains id of given report.
- Trigger is deleted from dbus, and its ReportNames property included id
  of given report.
- ReportNames property of any trigger is updated to include (or not) id
  of given report.

When this property is modified by service, signal will be emitted on
dbus for property change.

When there is existing trigger with id of non-existing report in its
ReportNames property, its id will be added to TriggerIds property of
such report, the moment it is created by user.

Testing done:
- new UTs were made, all UTs are passing.
- manual testing on dbus interface was successful.

Signed-off-by: Szymon Dompke <szymon.dompke@intel.com>
Change-Id: I1c4c94ce751ddaee001e3cadde3ea60aa8e1c224
diff --git a/src/trigger_manager.cpp b/src/trigger_manager.cpp
index c0b20d7..d581f71 100644
--- a/src/trigger_manager.cpp
+++ b/src/trigger_manager.cpp
@@ -8,6 +8,8 @@
 
 #include <phosphor-logging/log.hpp>
 
+#include <unordered_set>
+
 TriggerManager::TriggerManager(
     std::unique_ptr<interfaces::TriggerFactory> triggerFactoryIn,
     std::unique_ptr<interfaces::JsonStorage> triggerStorageIn,
@@ -51,8 +53,21 @@
         triggers.end());
 }
 
-void TriggerManager::verifyAddTrigger(const std::string& triggerId,
-                                      const std::string& triggerName) const
+void TriggerManager::verifyReportIds(
+    const std::vector<std::string>& newReportIds)
+{
+    if (std::unordered_set(newReportIds.begin(), newReportIds.end()).size() !=
+        newReportIds.size())
+    {
+        throw sdbusplus::exception::SdBusError(
+            static_cast<int>(std::errc::invalid_argument),
+            "Duplicate element in ReportIds");
+    }
+}
+
+void TriggerManager::verifyAddTrigger(
+    const std::string& triggerId, const std::string& triggerName,
+    const std::vector<std::string>& newReportIds) const
 {
     if (triggers.size() >= maxTriggers)
     {
@@ -71,6 +86,8 @@
                 static_cast<int>(std::errc::file_exists), "Duplicate trigger");
         }
     }
+
+    verifyReportIds(newReportIds);
 }
 
 interfaces::Trigger& TriggerManager::addTrigger(
@@ -87,7 +104,7 @@
         utils::generateId(triggerIdIn, triggerNameIn, triggerNameDefault,
                           existingTriggerIds, maxTriggerIdLength);
 
-    verifyAddTrigger(id, name);
+    verifyAddTrigger(id, name, reportIds);
 
     triggers.emplace_back(triggerFactory->make(
         id, name, triggerActions, reportIds, *this, *triggerStorage,
@@ -157,3 +174,19 @@
         }
     }
 }
+
+std::vector<std::string>
+    TriggerManager::getTriggerIdsForReport(const std::string& reportId) const
+{
+    std::vector<std::string> result;
+    for (const auto& trigger : triggers)
+    {
+        const auto& reportIds = trigger->getReportIds();
+        if (std::find(reportIds.begin(), reportIds.end(), reportId) !=
+            reportIds.end())
+        {
+            result.emplace_back(trigger->getId());
+        }
+    }
+    return result;
+}