Added support for Enabled property

Enabled property is added so that user can select via dbus interface if
Readings are updated and signal on Readings update is emitted, moreover
Metric calculation on received sensor data is active only when Enabled
is set to true

Tested:
- New unit tests were created, ran all new and previous UTs, all passed
- Tested under QEMU by adding reports for single and multiple sensors,
  changing Enabled property and emitting signal of value change for
  sensors added into the report, checking if Readings is accordingly
  updated or not updated
- Verified persistency, if Enabled property is successfully stored and
  restored after Telemetry service restart and also checked if after the
  restart dependencies of Enabled (Readings, Metric calculation) are
  properly initiated

Signed-off-by: Lukasz Kazmierczak <lukasz.kazmierczak@intel.com>
Change-Id: I29cf13693a48d15cb16d2ad6707f483f67f4879b
diff --git a/src/report.cpp b/src/report.cpp
index 8a7297e..af19a91 100644
--- a/src/report.cpp
+++ b/src/report.cpp
@@ -17,14 +17,15 @@
                const Milliseconds intervalIn,
                interfaces::ReportManager& reportManager,
                interfaces::JsonStorage& reportStorageIn,
-               std::vector<std::shared_ptr<interfaces::Metric>> metricsIn) :
+               std::vector<std::shared_ptr<interfaces::Metric>> metricsIn,
+               const bool enabledIn) :
     name(reportName),
     path(reportDir + name), reportingType(reportingTypeIn),
     interval(intervalIn), emitsReadingsUpdate(emitsReadingsUpdateIn),
     logToMetricReportsCollection(logToMetricReportsCollectionIn),
     objServer(objServer), metrics(std::move(metricsIn)), timer(ioc),
     fileName(std::to_string(std::hash<std::string>{}(name))),
-    reportStorage(reportStorageIn)
+    reportStorage(reportStorageIn), enabled(enabledIn)
 {
     readingParameters =
         toReadingParameters(utils::transform(metrics, [](const auto& metric) {
@@ -59,9 +60,12 @@
         scheduleTimer(interval);
     }
 
-    for (auto& metric : this->metrics)
+    if (enabled)
     {
-        metric->initialize();
+        for (auto& metric : this->metrics)
+        {
+            metric->initialize();
+        }
     }
 }
 
@@ -69,16 +73,50 @@
 {
     auto dbusIface = objServer->add_unique_interface(path, reportIfaceName);
     dbusIface->register_property_rw(
+        "Enabled", enabled, sdbusplus::vtable::property_::emits_change,
+        [this](bool newVal, const auto&) {
+            if (newVal != enabled)
+            {
+                if (true == newVal && "Periodic" == reportingType)
+                {
+                    scheduleTimer(interval);
+                }
+                if (newVal)
+                {
+                    for (auto& metric : metrics)
+                    {
+                        metric->initialize();
+                    }
+                }
+                else
+                {
+                    for (auto& metric : metrics)
+                    {
+                        metric->deinitialize();
+                    }
+                }
+
+                enabled = newVal;
+                persistency = storeConfiguration();
+            }
+            return true;
+        },
+        [this](const auto&) { return enabled; });
+    dbusIface->register_property_rw(
         "Interval", interval.count(),
         sdbusplus::vtable::property_::emits_change,
         [this](uint64_t newVal, auto&) {
-            Milliseconds newValT(newVal);
-            if (newValT < ReportManager::minInterval)
+            if (Milliseconds newValT{newVal};
+                newValT >= ReportManager::minInterval)
             {
-                return false;
+                if (newValT != interval)
+                {
+                    interval = newValT;
+                    persistency = storeConfiguration();
+                }
+                return true;
             }
-            interval = newValT;
-            return true;
+            return false;
         },
         [this](const auto&) { return interval.count(); });
     dbusIface->register_property_rw(
@@ -158,22 +196,24 @@
 
 void Report::updateReadings()
 {
-    using ReadingsValue = std::tuple_element_t<1, Readings>;
-    std::get<ReadingsValue>(cachedReadings).clear();
-
-    for (const auto& metric : metrics)
+    if (!enabled)
     {
-        for (const auto& reading : metric->getReadings())
-        {
-            std::get<1>(cachedReadings)
-                .emplace_back(reading.id, reading.metadata, reading.value,
-                              reading.timestamp);
-        }
+        return;
     }
 
     using ReadingsTimestamp = std::tuple_element_t<0, Readings>;
-    std::get<ReadingsTimestamp>(cachedReadings) = std::time(0);
+    using ReadingsValue = std::tuple_element_t<1, Readings>;
 
+    std::get<ReadingsValue>(cachedReadings).clear();
+    for (const auto& metric : metrics)
+    {
+        for (const auto& [id, meta, val, timestamp] : metric->getReadings())
+        {
+            std::get<ReadingsValue>(cachedReadings)
+                .emplace_back(id, meta, val, timestamp);
+        }
+    }
+    std::get<ReadingsTimestamp>(cachedReadings) = std::time(0);
     std::swap(readings, cachedReadings);
 
     reportIface->signal_property("Readings");
@@ -185,6 +225,7 @@
     {
         nlohmann::json data;
 
+        data["Enabled"] = enabled;
         data["Version"] = reportVersion;
         data["Name"] = name;
         data["ReportingType"] = reportingType;