Clean up triggers to use readJson

Use multiple level direct read

Tested: Visual only. Need help if anyone wants to test.

Change-Id: I2595a7024f1d02e02874310d1911cd4855b867be
Signed-off-by: Ed Tanous <ed@tanous.net>
diff --git a/redfish-core/lib/trigger.hpp b/redfish-core/lib/trigger.hpp
index a6d2254..43ec77d 100644
--- a/redfish-core/lib/trigger.hpp
+++ b/redfish-core/lib/trigger.hpp
@@ -123,31 +123,6 @@
     return resource::Health::Invalid;
 }
 
-inline std::string toDbusThresholdName(std::string_view redfishValue)
-{
-    if (redfishValue == "UpperCritical")
-    {
-        return "xyz.openbmc_project.Telemetry.Trigger.Type.UpperCritical";
-    }
-
-    if (redfishValue == "LowerCritical")
-    {
-        return "xyz.openbmc_project.Telemetry.Trigger.Type.LowerCritical";
-    }
-
-    if (redfishValue == "UpperWarning")
-    {
-        return "xyz.openbmc_project.Telemetry.Trigger.Type.UpperWarning";
-    }
-
-    if (redfishValue == "LowerWarning")
-    {
-        return "xyz.openbmc_project.Telemetry.Trigger.Type.LowerWarning";
-    }
-
-    return "";
-}
-
 inline std::string toRedfishThresholdName(std::string_view dbusValue)
 {
     if (dbusValue == "xyz.openbmc_project.Telemetry.Trigger.Type.UpperCritical")
@@ -294,61 +269,101 @@
     return std::nullopt;
 }
 
-inline bool parseNumericThresholds(crow::Response& res,
-                                   nlohmann::json& numericThresholds,
-                                   Context& ctx)
+inline bool parseThreshold(crow::Response& res,
+                           nlohmann::json::object_t& threshold,
+                           std::string_view dbusThresholdName,
+                           std::vector<NumericThresholdParams>& parsedParams)
 {
-    nlohmann::json::object_t* obj =
-        numericThresholds.get_ptr<nlohmann::json::object_t*>();
-    if (obj == nullptr)
+    double reading = 0.0;
+    std::string activation;
+    std::string dwellTimeStr;
+
+    if (!json_util::readJsonObject(threshold, res, "Reading", reading,
+                                   "Activation", activation, "DwellTime",
+                                   dwellTimeStr))
     {
-        messages::propertyValueTypeError(res, numericThresholds.dump(),
-                                         "NumericThresholds");
         return false;
     }
 
-    std::vector<NumericThresholdParams> parsedParams;
-    parsedParams.reserve(numericThresholds.size());
+    std::string dbusActivation = toDbusActivation(activation);
 
-    for (auto& key : *obj)
+    if (dbusActivation.empty())
     {
-        std::string dbusThresholdName = toDbusThresholdName(key.first);
-        if (dbusThresholdName.empty())
-        {
-            messages::propertyUnknown(res, key.first);
-            return false;
-        }
+        messages::propertyValueIncorrect(res, "Activation", activation);
+        return false;
+    }
 
-        double reading = 0.0;
-        std::string activation;
-        std::string dwellTimeStr;
+    std::optional<std::chrono::milliseconds> dwellTime =
+        time_utils::fromDurationString(dwellTimeStr);
+    if (!dwellTime)
+    {
+        messages::propertyValueIncorrect(res, "DwellTime", dwellTimeStr);
+        return false;
+    }
 
-        if (!json_util::readJson(key.second, res, "Reading", reading,
-                                 "Activation", activation, "DwellTime",
-                                 dwellTimeStr))
+    parsedParams.emplace_back(dbusThresholdName,
+                              static_cast<uint64_t>(dwellTime->count()),
+                              dbusActivation, reading);
+    return true;
+}
+
+struct NumericThresholds
+{
+    std::optional<nlohmann::json::object_t> upperCritical;
+    std::optional<nlohmann::json::object_t> upperWarning;
+    std::optional<nlohmann::json::object_t> lowerWarning;
+    std::optional<nlohmann::json::object_t> lowerCritical;
+
+    bool any() const
+    {
+        return upperCritical || upperWarning || lowerWarning || lowerCritical;
+    }
+};
+
+inline bool parseNumericThresholds(crow::Response& res,
+                                   NumericThresholds& numericThresholds,
+                                   Context& ctx)
+{
+    std::vector<NumericThresholdParams> parsedParams;
+    if (numericThresholds.upperCritical)
+    {
+        if (!parseThreshold(
+                res, *numericThresholds.upperCritical,
+                "xyz.openbmc_project.Telemetry.Trigger.Type.UpperCritical",
+                parsedParams))
         {
             return false;
         }
-
-        std::string dbusActivation = toDbusActivation(activation);
-
-        if (dbusActivation.empty())
+    }
+    if (numericThresholds.upperWarning)
+    {
+        if (!parseThreshold(
+                res, *numericThresholds.upperWarning,
+                "xyz.openbmc_project.Telemetry.Trigger.Type.UpperWarning",
+                parsedParams))
         {
-            messages::propertyValueIncorrect(res, "Activation", activation);
             return false;
         }
-
-        std::optional<std::chrono::milliseconds> dwellTime =
-            time_utils::fromDurationString(dwellTimeStr);
-        if (!dwellTime)
+    }
+    if (numericThresholds.lowerWarning)
+    {
+        if (!parseThreshold(
+                res, *numericThresholds.lowerWarning,
+                "xyz.openbmc_project.Telemetry.Trigger.Type.LowerWarning",
+                parsedParams))
         {
-            messages::propertyValueIncorrect(res, "DwellTime", dwellTimeStr);
             return false;
         }
-
-        parsedParams.emplace_back(dbusThresholdName,
-                                  static_cast<uint64_t>(dwellTime->count()),
-                                  dbusActivation, reading);
+    }
+    if (numericThresholds.lowerCritical)
+    {
+        if (!parseThreshold(
+                res, *numericThresholds.lowerCritical,
+                "xyz.openbmc_project.Telemetry.Trigger.Type.LowerCritical",
+                parsedParams))
+        {
+            return false;
+        }
     }
 
     ctx.thresholds = std::move(parsedParams);
@@ -357,7 +372,8 @@
 
 inline bool parseDiscreteTriggers(
     crow::Response& res,
-    std::optional<std::vector<nlohmann::json>>& discreteTriggers, Context& ctx)
+    std::optional<std::vector<nlohmann::json::object_t>>& discreteTriggers,
+    Context& ctx)
 {
     std::vector<DiscreteThresholdParams> parsedParams;
     if (!discreteTriggers)
@@ -367,16 +383,16 @@
     }
 
     parsedParams.reserve(discreteTriggers->size());
-    for (nlohmann::json& thresholdInfo : *discreteTriggers)
+    for (nlohmann::json::object_t& thresholdInfo : *discreteTriggers)
     {
         std::optional<std::string> name = "";
         std::string value;
         std::string dwellTimeStr;
         std::string severity;
 
-        if (!json_util::readJson(thresholdInfo, res, "Name", name, "Value",
-                                 value, "DwellTime", dwellTimeStr, "Severity",
-                                 severity))
+        if (!json_util::readJsonObject(thresholdInfo, res, "Name", name,
+                                       "Value", value, "DwellTime",
+                                       dwellTimeStr, "Severity", severity))
         {
             return false;
         }
@@ -407,10 +423,10 @@
 
 inline bool parseTriggerThresholds(
     crow::Response& res,
-    std::optional<std::vector<nlohmann::json>>& discreteTriggers,
-    std::optional<nlohmann::json>& numericThresholds, Context& ctx)
+    std::optional<std::vector<nlohmann::json::object_t>>& discreteTriggers,
+    NumericThresholds& numericThresholds, Context& ctx)
 {
-    if (discreteTriggers && numericThresholds)
+    if (discreteTriggers && numericThresholds.any())
     {
         messages::propertyValueConflict(res, "DiscreteTriggers",
                                         "NumericThresholds");
@@ -421,7 +437,7 @@
 
     if (ctx.discreteCondition)
     {
-        if (numericThresholds)
+        if (numericThresholds.any())
         {
             messages::propertyValueConflict(res, "DiscreteTriggerCondition",
                                             "NumericThresholds");
@@ -433,7 +449,7 @@
 
     if (ctx.metricType)
     {
-        if (*ctx.metricType == MetricType::Discrete && numericThresholds)
+        if (*ctx.metricType == MetricType::Discrete && numericThresholds.any())
         {
             messages::propertyValueConflict(res, "NumericThresholds",
                                             "MetricType");
@@ -481,9 +497,9 @@
             return false;
         }
     }
-    else if (numericThresholds)
+    else if (numericThresholds.any())
     {
-        if (!parseNumericThresholds(res, *numericThresholds, ctx))
+        if (!parseNumericThresholds(res, numericThresholds, ctx))
         {
             return false;
         }
@@ -498,35 +514,22 @@
     return true;
 }
 
-inline bool parseLinks(crow::Response& res, nlohmann::json& links, Context& ctx)
+inline bool parseLinks(crow::Response& res,
+                       const std::vector<std::string>& metricReportDefinitions,
+                       Context& ctx)
 {
-    if (links.empty())
+    ctx.reports.reserve(metricReportDefinitions.size());
+    for (const std::string& reportDefinionUri : metricReportDefinitions)
     {
-        return true;
-    }
-
-    std::optional<std::vector<std::string>> metricReportDefinitions;
-    if (!json_util::readJson(links, res, "MetricReportDefinitions",
-                             metricReportDefinitions))
-    {
-        return false;
-    }
-
-    if (metricReportDefinitions)
-    {
-        ctx.reports.reserve(metricReportDefinitions->size());
-        for (std::string& reportDefinionUri : *metricReportDefinitions)
+        std::optional<sdbusplus::message::object_path> reportPath =
+            getReportPathFromReportDefinitionUri(reportDefinionUri);
+        if (!reportPath)
         {
-            std::optional<sdbusplus::message::object_path> reportPath =
-                getReportPathFromReportDefinitionUri(reportDefinionUri);
-            if (!reportPath)
-            {
-                messages::propertyValueIncorrect(res, "MetricReportDefinitions",
-                                                 reportDefinionUri);
-                return false;
-            }
-            ctx.reports.emplace_back(*reportPath);
+            messages::propertyValueIncorrect(res, "MetricReportDefinitions",
+                                             reportDefinionUri);
+            return false;
         }
+        ctx.reports.emplace_back(*reportPath);
     }
     return true;
 }
@@ -588,18 +591,29 @@
     std::optional<std::string> metricType;
     std::optional<std::vector<std::string>> triggerActions;
     std::optional<std::string> discreteTriggerCondition;
-    std::optional<std::vector<nlohmann::json>> discreteTriggers;
-    std::optional<nlohmann::json> numericThresholds;
-    std::optional<nlohmann::json> links;
+    std::optional<std::vector<nlohmann::json::object_t>> discreteTriggers;
+    std::optional<std::vector<std::string>> metricReportDefinitions;
+    NumericThresholds thresholds;
+    // clang-format off
     if (!json_util::readJsonPatch(
-            req, res, "Id", id, "Name", name, "MetricType", metricType,
-            "TriggerActions", triggerActions, "DiscreteTriggerCondition",
-            discreteTriggerCondition, "DiscreteTriggers", discreteTriggers,
-            "NumericThresholds", numericThresholds, "MetricProperties",
-            ctx.metricProperties, "Links", links))
+            req, res,
+	    "Id", id,
+            "Name", name,
+	    "MetricType", metricType,
+            "TriggerActions", triggerActions,
+            "DiscreteTriggerCondition", discreteTriggerCondition,
+            "DiscreteTriggers", discreteTriggers,
+            "NumericThresholds/UpperCritical", thresholds.upperCritical,
+            "NumericThresholds/UpperWarning", thresholds.upperWarning,
+            "NumericThresholds/LowerWarning", thresholds.lowerWarning,
+            "NumericThresholds/LowerCritical", thresholds.lowerCritical,
+            "MetricProperties", ctx.metricProperties,
+            "Links/MetricReportDefinitions", metricReportDefinitions)
+    )
     {
         return false;
     }
+    // clang-format on
 
     ctx.id = *id;
     ctx.name = *name;
@@ -646,14 +660,14 @@
         return false;
     }
 
-    if (!parseTriggerThresholds(res, discreteTriggers, numericThresholds, ctx))
+    if (!parseTriggerThresholds(res, discreteTriggers, thresholds, ctx))
     {
         return false;
     }
 
-    if (links)
+    if (metricReportDefinitions)
     {
-        if (!parseLinks(res, *links, ctx))
+        if (!parseLinks(res, *metricReportDefinitions, ctx))
         {
             return false;
         }