Move threshold interfaces to common function

Threshold Hardshutdown and Softshutdown interfaces needs to be
created for sensors. Therefore, created a common function
called getInterface() to access all the threshold
interfaces from all the sensors source file and removed
hasCriticalInterface(), hasWarningInterface() functions.

Moreover, threshold interfaces from all the sensor constructor has
been refactored to avoid duplicating functions.

TESTED: Tested on Facebook YosemiteV2 hardware. Verified that the
Warning and Critical interfaces are created and displaying in
dbus objects.

Signed-off-by: Jayashree Dhanapal <jayashree-d@hcl.com>
Change-Id: I9c7d049c7d4445d7199bf63d8e729838990880e9
diff --git a/src/Thresholds.cpp b/src/Thresholds.cpp
index 20f7949..bc6bb84 100644
--- a/src/Thresholds.cpp
+++ b/src/Thresholds.cpp
@@ -44,6 +44,18 @@
     return Direction::ERROR;
 }
 
+bool findOrder(Level lev, Direction dir)
+{
+    for (Sensor::ThresholdProperty prop : Sensor::thresProp)
+    {
+        if ((prop.level == lev) && (prop.direction == dir))
+        {
+            return true;
+        }
+    }
+    return false;
+}
+
 bool parseThresholdsFromConfig(
     const SensorData& sensorData,
     std::vector<thresholds::Threshold>& thresholdVector,
@@ -209,19 +221,13 @@
 
     for (const auto& threshold : sensor->thresholds)
     {
-        std::shared_ptr<sdbusplus::asio::dbus_interface> interface;
-        if (threshold.level == thresholds::Level::CRITICAL)
-        {
-            interface = sensor->thresholdInterfaceCritical;
-        }
-        else if (threshold.level == thresholds::Level::WARNING)
-        {
-            interface = sensor->thresholdInterfaceWarning;
-        }
-        else
+        if (!findOrder(threshold.level, threshold.direction))
         {
             continue;
         }
+        std::shared_ptr<sdbusplus::asio::dbus_interface> interface =
+            sensor->getThresholdInterface(threshold.level);
+
         if (!interface)
         {
             continue;
@@ -455,21 +461,14 @@
                       thresholds::Level level, thresholds::Direction direction,
                       bool assert)
 {
-    std::shared_ptr<sdbusplus::asio::dbus_interface> interface;
-    if (level == thresholds::Level::WARNING)
+    if (!findOrder(level, direction))
     {
-        interface = sensor->thresholdInterfaceWarning;
-    }
-    else if (level == thresholds::Level::CRITICAL)
-    {
-        interface = sensor->thresholdInterfaceCritical;
-    }
-    else
-    {
-        std::cerr << "Unknown threshold, level " << static_cast<int>(level)
-                  << "direction " << static_cast<int>(direction) << "\n";
         return;
     }
+
+    std::shared_ptr<sdbusplus::asio::dbus_interface> interface =
+        sensor->getThresholdInterface(level);
+
     if (!interface)
     {
         std::cout << "trying to set uninitialized interface\n";
@@ -554,29 +553,22 @@
     return true;
 }
 
-bool hasCriticalInterface(
-    const std::vector<thresholds::Threshold>& thresholdVector)
+std::string getInterface(const Level thresholdLevel)
 {
-    for (auto& threshold : thresholdVector)
+    std::string level;
+    switch (thresholdLevel)
     {
-        if (threshold.level == Level::CRITICAL)
-        {
-            return true;
-        }
+        case Level::WARNING:
+            level = "Warning";
+            break;
+        case Level::CRITICAL:
+            level = "Critical";
+            break;
+        case Level::ERROR:
+            level = "Error";
+            break;
     }
-    return false;
-}
-
-bool hasWarningInterface(
-    const std::vector<thresholds::Threshold>& thresholdVector)
-{
-    for (auto& threshold : thresholdVector)
-    {
-        if (threshold.level == Level::WARNING)
-        {
-            return true;
-        }
-    }
-    return false;
+    std::string interface = "xyz.openbmc_project.Sensor.Threshold." + level;
+    return interface;
 }
 } // namespace thresholds