Trigger: make dbus properties writable

This change allows to modify 'Sensors', 'ReportNames' and 'Thresholds'
dbus properties of Trigger interface. They are required by Redfish to
implement PATCH functionality for Trigger schema.

Some backend changes were required to enable this functionality, and as
such few improvements were made for existing code:
- NumericThreshold and DiscreteThreshold now have common implementation
  where it was possible.
- Internal sensor info structure for Trigger is now the same as the one
  used for Report. This resulted in breaking compatibility with previous
  Trigger persistency data.
- Added getInfo / getParams methods for Sensor and Threshold interfaces.
  They are used by Trigger dbus getters and persistency mechanism now,
  instead of storing this data in Trigger object.

Testing done:
- Unit tests were expanded and are passing
- dbus setters for Sensors and Thresholds are working and modifications
  are reflected by calling appropriate getters.

Signed-off-by: Szymon Dompke <szymon.dompke@intel.com>
Change-Id: I7a14c15a30d78ce872342b5f938aba43c77be9c0
diff --git a/src/numeric_threshold.cpp b/src/numeric_threshold.cpp
index 3bec427..f152e76 100644
--- a/src/numeric_threshold.cpp
+++ b/src/numeric_threshold.cpp
@@ -4,41 +4,40 @@
 
 NumericThreshold::NumericThreshold(
     boost::asio::io_context& ioc, Sensors sensorsIn,
-    std::vector<std::string> sensorNames,
     std::vector<std::unique_ptr<interfaces::TriggerAction>> actionsIn,
-    Milliseconds dwellTimeIn, numeric::Direction direction,
-    double thresholdValueIn) :
+    Milliseconds dwellTimeIn, numeric::Direction directionIn,
+    double thresholdValueIn, numeric::Type typeIn) :
     ioc(ioc),
-    sensors(std::move(sensorsIn)), actions(std::move(actionsIn)),
-    dwellTime(dwellTimeIn), direction(direction),
-    thresholdValue(thresholdValueIn)
+    actions(std::move(actionsIn)), dwellTime(dwellTimeIn),
+    direction(directionIn), thresholdValue(thresholdValueIn), type(typeIn)
 {
-    details.reserve(sensors.size());
-    for (size_t i = 0; i < sensors.size(); i++)
+    for (const auto& sensor : sensorsIn)
     {
-        details.emplace_back(sensorNames[i], thresholdValue, false, ioc);
+        sensorDetails.emplace(sensor, makeDetails(sensor->getName()));
     }
 }
 
-NumericThreshold::~NumericThreshold()
-{}
-
 void NumericThreshold::initialize()
 {
-    for (auto& sensor : sensors)
-    {
-        sensor->registerForUpdates(weak_from_this());
-    }
+    ThresholdOperations().initialize(this);
+}
+
+void NumericThreshold::updateSensors(Sensors newSensors)
+{
+    ThresholdOperations().updateSensors(this, std::move(newSensors));
 }
 
 NumericThreshold::ThresholdDetail&
-    NumericThreshold::getDetails(interfaces::Sensor& sensor)
+    NumericThreshold::getDetails(const interfaces::Sensor& sensor)
 {
-    auto it =
-        std::find_if(sensors.begin(), sensors.end(),
-                     [&sensor](const auto& x) { return &sensor == x.get(); });
-    auto index = std::distance(sensors.begin(), it);
-    return details.at(index);
+    return ThresholdOperations().getDetails(this, sensor);
+}
+
+std::shared_ptr<NumericThreshold::ThresholdDetail>
+    NumericThreshold::makeDetails(const std::string& sensorName)
+{
+    return std::make_shared<ThresholdDetail>(sensorName, thresholdValue, false,
+                                             ioc);
 }
 
 void NumericThreshold::sensorUpdated(interfaces::Sensor& sensor,
@@ -48,7 +47,8 @@
 void NumericThreshold::sensorUpdated(interfaces::Sensor& sensor,
                                      Milliseconds timestamp, double value)
 {
-    auto& [sensorName, prevValue, dwell, timer] = getDetails(sensor);
+    auto& details = getDetails(sensor);
+    auto& [sensorName, prevValue, dwell, timer] = details;
     bool decreasing = thresholdValue < prevValue && thresholdValue > value;
     bool increasing = thresholdValue > prevValue && thresholdValue < value;
 
@@ -61,16 +61,19 @@
         (direction == numeric::Direction::increasing && increasing) ||
         (direction == numeric::Direction::either && (increasing || decreasing)))
     {
-        startTimer(sensorName, timestamp, value, dwell, timer);
+        startTimer(details, timestamp, value);
     }
 
     prevValue = value;
 }
 
-void NumericThreshold::startTimer(const std::string& sensorName,
-                                  Milliseconds timestamp, double value,
-                                  bool& dwell, boost::asio::steady_timer& timer)
+void NumericThreshold::startTimer(NumericThreshold::ThresholdDetail& details,
+                                  Milliseconds timestamp, double value)
 {
+    const auto& sensorName = details.sensorName;
+    auto& dwell = details.dwell;
+    auto& timer = details.timer;
+
     if (dwellTime == Milliseconds::zero())
     {
         commit(sensorName, timestamp, value);
@@ -79,8 +82,8 @@
     {
         dwell = true;
         timer.expires_after(dwellTime);
-        timer.async_wait([this, sensorName, timestamp, value,
-                          &dwell](const boost::system::error_code ec) {
+        timer.async_wait([this, &sensorName, &dwell, timestamp,
+                          value](const boost::system::error_code ec) {
             if (ec)
             {
                 phosphor::logging::log<phosphor::logging::level::DEBUG>(
@@ -101,3 +104,9 @@
         action->commit(sensorName, timestamp, value);
     }
 }
+
+LabeledThresholdParam NumericThreshold::getThresholdParam() const
+{
+    return numeric::LabeledThresholdParam(type, dwellTime.count(), direction,
+                                          thresholdValue);
+}