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.cpp b/src/trigger.cpp
index db988ff..34609b3 100644
--- a/src/trigger.cpp
+++ b/src/trigger.cpp
@@ -17,13 +17,15 @@
     std::vector<std::shared_ptr<interfaces::Threshold>>&& thresholdsIn,
     interfaces::TriggerManager& triggerManager,
     interfaces::JsonStorage& triggerStorageIn,
-    const interfaces::TriggerFactory& triggerFactory, Sensors sensorsIn) :
+    const interfaces::TriggerFactory& triggerFactory, Sensors sensorsIn,
+    interfaces::ReportManager& reportManagerIn) :
     id(idIn),
     name(nameIn), triggerActions(std::move(triggerActionsIn)),
     path(triggerDir + id), reportIds(std::move(reportIdsIn)),
     thresholds(std::move(thresholdsIn)),
     fileName(std::to_string(std::hash<std::string>{}(id))),
-    triggerStorage(triggerStorageIn), sensors(std::move(sensorsIn))
+    triggerStorage(triggerStorageIn), sensors(std::move(sensorsIn)),
+    reportManager(reportManagerIn)
 {
     deleteIface = objServer->add_unique_interface(
         path, deleteIfaceName, [this, &ioc, &triggerManager](auto& dbusIface) {
@@ -32,6 +34,11 @@
                 {
                     triggerStorage.remove(fileName);
                 }
+                for (const auto& reportId : *reportIds)
+                {
+                    reportManager.updateTriggerIds(reportId, id,
+                                                   TriggerIdUpdate::Remove);
+                }
                 boost::asio::post(ioc, [this, &triggerManager] {
                     triggerManager.removeTrigger(this);
                 });
@@ -106,6 +113,8 @@
                 "ReportNames", std::vector<std::string>{},
                 sdbusplus::vtable::property_::emits_change,
                 [this](auto newVal, auto& oldVal) {
+                    TriggerManager::verifyReportIds(newVal);
+                    updateTriggerIdsInReports(newVal);
                     reportIds->clear();
                     std::copy(newVal.begin(), newVal.end(),
                               std::back_inserter(*reportIds));
@@ -145,6 +154,11 @@
     {
         threshold->initialize();
     }
+
+    for (const auto& reportId : *reportIds)
+    {
+        reportManager.updateTriggerIds(reportId, id, TriggerIdUpdate::Add);
+    }
 }
 
 bool Trigger::storeConfiguration() const
@@ -186,3 +200,26 @@
     }
     return true;
 }
+
+void Trigger::updateTriggerIdsInReports(
+    const std::vector<std::string>& newReportIds)
+{
+    std::vector<std::string> toBeRemoved, toBeAdded;
+    size_t maxSize = std::max(reportIds->size(), newReportIds.size());
+    toBeRemoved.reserve(maxSize);
+    toBeAdded.reserve(maxSize);
+    std::set_difference(reportIds->begin(), reportIds->end(),
+                        newReportIds.begin(), newReportIds.end(),
+                        std::back_inserter(toBeRemoved));
+    std::set_difference(newReportIds.begin(), newReportIds.end(),
+                        reportIds->begin(), reportIds->end(),
+                        std::back_inserter(toBeAdded));
+    for (auto& reportId : toBeRemoved)
+    {
+        reportManager.updateTriggerIds(reportId, id, TriggerIdUpdate::Remove);
+    }
+    for (auto& reportId : toBeAdded)
+    {
+        reportManager.updateTriggerIds(reportId, id, TriggerIdUpdate::Add);
+    }
+}