Add support for AppendLimit and ReportUpdates

Added 2 new properties for Report interface: AppendLimit and
ReportUpdates. They were also added as arguments to the future version
of AddReport method of ReportManager.

ReportUpdates property defines the report update behavior:
- Overwrite: Each report update overrides previous "Readings" property.
  "AppendLimit" set by user is not respected - "Readings" property size
  is equal to count of all sensor across all metrics defined in report.
- AppendWrapsWhenFull: New readings are appended until limit specified
  by "AppendLimit" is reached. Then oldest readings are overwritten by
  new ones.
- AppendStopsWhenFull: New readings are appended until limit specified
  by "AppendLimit" is reached. Then updates are stopped.
- NewReport: not supported yet and will be implemented in the future.

Please note that if ReportingType is set to OnRequest, those 2 new
properties are ignored, and Readings property will contain one reading
per defined sensor, across all metrics. They are still stored, which
means that if ReportingType will be changed in the runtime, those
properties will be respected.

Tested:
- Both new properties can be accessed from dbus.
- Both properties are reflected in Readings property.
- Old AddReport method is working as before the change.
- UTs are passing.

Signed-off-by: Szymon Dompke <szymon.dompke@intel.com>
Change-Id: I8a18f7e68215f0f6e5c403b533d2c4ff479df69e
diff --git a/src/report_manager.cpp b/src/report_manager.cpp
index 64c587e..21a9736 100644
--- a/src/report_manager.cpp
+++ b/src/report_manager.cpp
@@ -57,31 +57,39 @@
                                     const uint64_t interval,
                                     ReadingParametersPastVersion metricParams) {
                     constexpr auto enabledDefault = true;
-                    return addReport(yield, reportName, reportingType,
-                                     emitsReadingsUpdate,
-                                     logToMetricReportsCollection,
-                                     Milliseconds(interval),
-                                     convertToReadingParameters(
-                                         std::move(metricParams)),
-                                     enabledDefault)
+                    constexpr uint64_t appendLimitDefault = 0;
+                    constexpr ReportUpdates reportUpdatesDefault =
+                        ReportUpdates::Overwrite;
+                    return addReport(
+                               yield, reportName, reportingType,
+                               emitsReadingsUpdate,
+                               logToMetricReportsCollection,
+                               Milliseconds(interval), appendLimitDefault,
+                               reportUpdatesToString(reportUpdatesDefault),
+                               convertToReadingParameters(
+                                   std::move(metricParams)),
+                               enabledDefault)
                         .getPath();
                 });
 
             dbusIface.register_method(
                 "AddReportFutureVersion",
-                [this](boost::asio::yield_context& yield,
-                       const std::string& reportName,
-                       const std::string& reportingType,
-                       const bool emitsReadingsUpdate,
-                       const bool logToMetricReportsCollection,
-                       const uint64_t interval,
-                       ReadingParameters metricParams) {
+
+                [this](
+                    boost::asio::yield_context& yield,
+                    const std::string& reportName,
+                    const std::string& reportingType,
+                    const std::string& reportUpdates,
+                    const uint64_t appendLimit, const bool emitsReadingsUpdate,
+                    const bool logToMetricReportsCollection,
+                    const uint64_t interval, ReadingParameters metricParams) {
                     constexpr auto enabledDefault = true;
                     return addReport(yield, reportName, reportingType,
                                      emitsReadingsUpdate,
                                      logToMetricReportsCollection,
-                                     Milliseconds(interval),
-                                     std::move(metricParams), enabledDefault)
+                                     Milliseconds(interval), appendLimit,
+                                     reportUpdates, std::move(metricParams),
+                                     enabledDefault)
                         .getPath();
                 });
         });
@@ -107,7 +115,7 @@
 
 void ReportManager::verifyAddReport(
     const std::string& reportName, const std::string& reportingType,
-    Milliseconds interval,
+    Milliseconds interval, const std::string& reportUpdates,
     const std::vector<LabeledMetricParameters>& readingParams)
 {
     if (reports.size() >= maxReports)
@@ -128,15 +136,16 @@
         }
     }
 
-    auto found = std::find(supportedReportingType.begin(),
-                           supportedReportingType.end(), reportingType);
-    if (found == supportedReportingType.end())
+    if (std::find(supportedReportingType.begin(), supportedReportingType.end(),
+                  reportingType) == supportedReportingType.end())
     {
         throw sdbusplus::exception::SdBusError(
             static_cast<int>(std::errc::invalid_argument),
             "Invalid reportingType");
     }
 
+    verifyReportUpdates(reportUpdates);
+
     if (reportingType == "Periodic" && interval < minInterval)
     {
         throw sdbusplus::exception::SdBusError(
@@ -172,29 +181,32 @@
     boost::asio::yield_context& yield, const std::string& reportName,
     const std::string& reportingType, const bool emitsReadingsUpdate,
     const bool logToMetricReportsCollection, Milliseconds interval,
+    const uint64_t appendLimit, const std::string& reportUpdates,
     ReadingParameters metricParams, const bool enabled)
 {
     auto labeledMetricParams =
         reportFactory->convertMetricParams(yield, metricParams);
 
     return addReport(reportName, reportingType, emitsReadingsUpdate,
-                     logToMetricReportsCollection, interval,
-                     std::move(labeledMetricParams), enabled);
+                     logToMetricReportsCollection, interval, appendLimit,
+                     reportUpdates, std::move(labeledMetricParams), enabled);
 }
 
 interfaces::Report& ReportManager::addReport(
     const std::string& reportName, const std::string& reportingType,
     const bool emitsReadingsUpdate, const bool logToMetricReportsCollection,
-    Milliseconds interval,
+    Milliseconds interval, const uint64_t appendLimit,
+    const std::string& reportUpdates,
     std::vector<LabeledMetricParameters> labeledMetricParams,
     const bool enabled)
 {
-    verifyAddReport(reportName, reportingType, interval, labeledMetricParams);
+    verifyAddReport(reportName, reportingType, interval, reportUpdates,
+                    labeledMetricParams);
 
-    reports.emplace_back(
-        reportFactory->make(reportName, reportingType, emitsReadingsUpdate,
-                            logToMetricReportsCollection, interval, *this,
-                            *reportStorage, labeledMetricParams, enabled));
+    reports.emplace_back(reportFactory->make(
+        reportName, reportingType, emitsReadingsUpdate,
+        logToMetricReportsCollection, interval, appendLimit, reportUpdates,
+        *this, *reportStorage, labeledMetricParams, enabled));
     return *reports.back();
 }
 
@@ -222,13 +234,17 @@
             bool logToMetricReportsCollection =
                 data->at("LogToMetricReportsCollection").get<bool>();
             uint64_t interval = data->at("Interval").get<uint64_t>();
+            uint64_t appendLimit = data->at("AppendLimit").get<uint64_t>();
+            std::string reportUpdates =
+                data->at("ReportUpdates").get<std::string>();
             auto readingParameters =
                 data->at("ReadingParameters")
                     .get<std::vector<LabeledMetricParameters>>();
 
             addReport(name, reportingType, emitsReadingsSignal,
                       logToMetricReportsCollection, Milliseconds(interval),
-                      std::move(readingParameters), enabled);
+                      appendLimit, reportUpdates, std::move(readingParameters),
+                      enabled);
         }
         catch (const std::exception& e)
         {
@@ -254,3 +270,14 @@
         }
     }
 }
+
+void ReportManager::verifyReportUpdates(const std::string& reportUpdates)
+{
+    if (std::find(supportedReportUpdates.begin(), supportedReportUpdates.end(),
+                  reportUpdates) == supportedReportUpdates.end())
+    {
+        throw sdbusplus::exception::SdBusError(
+            static_cast<int>(std::errc::invalid_argument),
+            "Invalid ReportUpdates");
+    }
+}