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/ADCSensor.cpp b/src/ADCSensor.cpp
index 9758168..aaef76d 100644
--- a/src/ADCSensor.cpp
+++ b/src/ADCSensor.cpp
@@ -62,17 +62,12 @@
     sensorInterface = objectServer.add_interface(
         "/xyz/openbmc_project/sensors/voltage/" + name,
         "xyz.openbmc_project.Sensor.Value");
-    if (thresholds::hasWarningInterface(thresholds))
+    for (const auto& threshold : thresholds)
     {
-        thresholdInterfaceWarning = objectServer.add_interface(
-            "/xyz/openbmc_project/sensors/voltage/" + name,
-            "xyz.openbmc_project.Sensor.Threshold.Warning");
-    }
-    if (thresholds::hasCriticalInterface(thresholds))
-    {
-        thresholdInterfaceCritical = objectServer.add_interface(
-            "/xyz/openbmc_project/sensors/voltage/" + name,
-            "xyz.openbmc_project.Sensor.Threshold.Critical");
+        std::string interface = thresholds::getInterface(threshold.level);
+        thresholdInterfaces[static_cast<size_t>(threshold.level)] =
+            objectServer.add_interface(
+                "/xyz/openbmc_project/sensors/voltage/" + name, interface);
     }
     association = objectServer.add_interface(
         "/xyz/openbmc_project/sensors/voltage/" + name, association::interface);
@@ -85,8 +80,10 @@
     inputDev.close();
     waitTimer.cancel();
 
-    objServer.remove_interface(thresholdInterfaceWarning);
-    objServer.remove_interface(thresholdInterfaceCritical);
+    for (const auto& iface : thresholdInterfaces)
+    {
+        objServer.remove_interface(iface);
+    }
     objServer.remove_interface(sensorInterface);
     objServer.remove_interface(association);
 }
diff --git a/src/CPUSensor.cpp b/src/CPUSensor.cpp
index cd1038f..d57d565 100644
--- a/src/CPUSensor.cpp
+++ b/src/CPUSensor.cpp
@@ -75,17 +75,12 @@
 
             sensorInterface = objectServer.add_interface(
                 interfacePath, "xyz.openbmc_project.Sensor.Value");
-            if (thresholds::hasWarningInterface(thresholds))
+            for (const auto& threshold : thresholds)
             {
-                thresholdInterfaceWarning = objectServer.add_interface(
-                    interfacePath,
-                    "xyz.openbmc_project.Sensor.Threshold.Warning");
-            }
-            if (thresholds::hasCriticalInterface(thresholds))
-            {
-                thresholdInterfaceCritical = objectServer.add_interface(
-                    interfacePath,
-                    "xyz.openbmc_project.Sensor.Threshold.Critical");
+                std::string interface =
+                    thresholds::getInterface(threshold.level);
+                thresholdInterfaces[static_cast<size_t>(threshold.level)] =
+                    objectServer.add_interface(interfacePath, interface);
             }
             association = objectServer.add_interface(interfacePath,
                                                      association::interface);
@@ -105,8 +100,10 @@
     waitTimer.cancel();
     if (show)
     {
-        objServer.remove_interface(thresholdInterfaceWarning);
-        objServer.remove_interface(thresholdInterfaceCritical);
+        for (const auto& iface : thresholdInterfaces)
+        {
+            objServer.remove_interface(iface);
+        }
         objServer.remove_interface(sensorInterface);
         objServer.remove_interface(association);
         objServer.remove_interface(availableInterface);
diff --git a/src/ExitAirTempSensor.cpp b/src/ExitAirTempSensor.cpp
index a36c183..fd31473 100644
--- a/src/ExitAirTempSensor.cpp
+++ b/src/ExitAirTempSensor.cpp
@@ -172,17 +172,12 @@
         "/xyz/openbmc_project/sensors/airflow/" + name,
         "xyz.openbmc_project.Sensor.Value");
 
-    if (thresholds::hasWarningInterface(thresholds))
+    for (const auto& threshold : thresholds)
     {
-        thresholdInterfaceWarning = objectServer.add_interface(
-            "/xyz/openbmc_project/sensors/airflow/" + name,
-            "xyz.openbmc_project.Sensor.Threshold.Warning");
-    }
-    if (thresholds::hasCriticalInterface(thresholds))
-    {
-        thresholdInterfaceCritical = objectServer.add_interface(
-            "/xyz/openbmc_project/sensors/airflow/" + name,
-            "xyz.openbmc_project.Sensor.Threshold.Critical");
+        std::string interface = thresholds::getInterface(threshold.level);
+        thresholdInterfaces[static_cast<size_t>(threshold.level)] =
+            objectServer.add_interface(
+                "/xyz/openbmc_project/sensors/airflow/" + name, interface);
     }
 
     association = objectServer.add_interface(
@@ -290,8 +285,10 @@
 
 CFMSensor::~CFMSensor()
 {
-    objServer.remove_interface(thresholdInterfaceWarning);
-    objServer.remove_interface(thresholdInterfaceCritical);
+    for (const auto& iface : thresholdInterfaces)
+    {
+        objServer.remove_interface(iface);
+    }
     objServer.remove_interface(sensorInterface);
     objServer.remove_interface(association);
     objServer.remove_interface(cfmLimitIface);
@@ -519,17 +516,12 @@
         "/xyz/openbmc_project/sensors/temperature/" + name,
         "xyz.openbmc_project.Sensor.Value");
 
-    if (thresholds::hasWarningInterface(thresholds))
+    for (const auto& threshold : thresholds)
     {
-        thresholdInterfaceWarning = objectServer.add_interface(
-            "/xyz/openbmc_project/sensors/temperature/" + name,
-            "xyz.openbmc_project.Sensor.Threshold.Warning");
-    }
-    if (thresholds::hasCriticalInterface(thresholds))
-    {
-        thresholdInterfaceCritical = objectServer.add_interface(
-            "/xyz/openbmc_project/sensors/temperature/" + name,
-            "xyz.openbmc_project.Sensor.Threshold.Critical");
+        std::string interface = thresholds::getInterface(threshold.level);
+        thresholdInterfaces[static_cast<size_t>(threshold.level)] =
+            objectServer.add_interface(
+                "/xyz/openbmc_project/sensors/temperature/" + name, interface);
     }
     association = objectServer.add_interface(
         "/xyz/openbmc_project/sensors/temperature/" + name,
@@ -539,8 +531,10 @@
 
 ExitAirTempSensor::~ExitAirTempSensor()
 {
-    objServer.remove_interface(thresholdInterfaceWarning);
-    objServer.remove_interface(thresholdInterfaceCritical);
+    for (const auto& iface : thresholdInterfaces)
+    {
+        objServer.remove_interface(iface);
+    }
     objServer.remove_interface(sensorInterface);
     objServer.remove_interface(association);
 }
diff --git a/src/ExternalSensor.cpp b/src/ExternalSensor.cpp
index 961206c..3401ac4 100644
--- a/src/ExternalSensor.cpp
+++ b/src/ExternalSensor.cpp
@@ -51,15 +51,11 @@
     sensorInterface = objectServer.add_interface(
         objectPath, "xyz.openbmc_project.Sensor.Value");
 
-    if (thresholds::hasWarningInterface(thresholds))
+    for (const auto& threshold : thresholds)
     {
-        thresholdInterfaceWarning = objectServer.add_interface(
-            objectPath, "xyz.openbmc_project.Sensor.Threshold.Warning");
-    }
-    if (thresholds::hasCriticalInterface(thresholds))
-    {
-        thresholdInterfaceCritical = objectServer.add_interface(
-            objectPath, "xyz.openbmc_project.Sensor.Threshold.Critical");
+        std::string interface = thresholds::getInterface(threshold.level);
+        thresholdInterfaces[static_cast<size_t>(threshold.level)] =
+            objectServer.add_interface(objectPath, interface);
     }
 
     association =
@@ -109,8 +105,10 @@
     externalSetHook = nullptr;
 
     objServer.remove_interface(association);
-    objServer.remove_interface(thresholdInterfaceCritical);
-    objServer.remove_interface(thresholdInterfaceWarning);
+    for (const auto& iface : thresholdInterfaces)
+    {
+        objServer.remove_interface(iface);
+    }
     objServer.remove_interface(sensorInterface);
 
     if constexpr (debug)
diff --git a/src/HwmonTempSensor.cpp b/src/HwmonTempSensor.cpp
index ba632a2..9871a5b 100644
--- a/src/HwmonTempSensor.cpp
+++ b/src/HwmonTempSensor.cpp
@@ -62,19 +62,14 @@
             name,
         "xyz.openbmc_project.Sensor.Value");
 
-    if (thresholds::hasWarningInterface(thresholds))
+    for (const auto& threshold : thresholds)
     {
-        thresholdInterfaceWarning = objectServer.add_interface(
-            "/xyz/openbmc_project/sensors/" + thisSensorParameters.typeName +
-                "/" + name,
-            "xyz.openbmc_project.Sensor.Threshold.Warning");
-    }
-    if (thresholds::hasCriticalInterface(thresholds))
-    {
-        thresholdInterfaceCritical = objectServer.add_interface(
-            "/xyz/openbmc_project/sensors/" + thisSensorParameters.typeName +
-                "/" + name,
-            "xyz.openbmc_project.Sensor.Threshold.Critical");
+        std::string interface = thresholds::getInterface(threshold.level);
+        thresholdInterfaces[static_cast<size_t>(threshold.level)] =
+            objectServer.add_interface("/xyz/openbmc_project/sensors/" +
+                                           thisSensorParameters.typeName + "/" +
+                                           name,
+                                       interface);
     }
     association = objectServer.add_interface("/xyz/openbmc_project/sensors/" +
                                                  thisSensorParameters.typeName +
@@ -88,8 +83,10 @@
     // close the input dev to cancel async operations
     inputDev.close();
     waitTimer.cancel();
-    objServer.remove_interface(thresholdInterfaceWarning);
-    objServer.remove_interface(thresholdInterfaceCritical);
+    for (const auto& iface : thresholdInterfaces)
+    {
+        objServer.remove_interface(iface);
+    }
     objServer.remove_interface(sensorInterface);
     objServer.remove_interface(association);
 }
diff --git a/src/IpmbSensor.cpp b/src/IpmbSensor.cpp
index eedf21e..b2c9dd2 100644
--- a/src/IpmbSensor.cpp
+++ b/src/IpmbSensor.cpp
@@ -76,15 +76,11 @@
     sensorInterface = objectServer.add_interface(
         dbusPath, "xyz.openbmc_project.Sensor.Value");
 
-    if (thresholds::hasWarningInterface(thresholds))
+    for (const auto& threshold : thresholds)
     {
-        thresholdInterfaceWarning = objectServer.add_interface(
-            dbusPath, "xyz.openbmc_project.Sensor.Threshold.Warning");
-    }
-    if (thresholds::hasCriticalInterface(thresholds))
-    {
-        thresholdInterfaceCritical = objectServer.add_interface(
-            dbusPath, "xyz.openbmc_project.Sensor.Threshold.Critical");
+        std::string interface = thresholds::getInterface(threshold.level);
+        thresholdInterfaces[static_cast<size_t>(threshold.level)] =
+            objectServer.add_interface(dbusPath, interface);
     }
     association = objectServer.add_interface(dbusPath, association::interface);
 }
@@ -92,8 +88,10 @@
 IpmbSensor::~IpmbSensor()
 {
     waitTimer.cancel();
-    objectServer.remove_interface(thresholdInterfaceWarning);
-    objectServer.remove_interface(thresholdInterfaceCritical);
+    for (const auto& iface : thresholdInterfaces)
+    {
+        objectServer.remove_interface(iface);
+    }
     objectServer.remove_interface(sensorInterface);
     objectServer.remove_interface(association);
 }
diff --git a/src/MCUTempSensor.cpp b/src/MCUTempSensor.cpp
index 4f8c848..79a2959 100644
--- a/src/MCUTempSensor.cpp
+++ b/src/MCUTempSensor.cpp
@@ -65,17 +65,12 @@
         "/xyz/openbmc_project/sensors/temperature/" + name,
         "xyz.openbmc_project.Sensor.Value");
 
-    if (thresholds::hasWarningInterface(thresholds))
+    for (const auto& threshold : thresholds)
     {
-        thresholdInterfaceWarning = objectServer.add_interface(
-            "/xyz/openbmc_project/sensors/temperature/" + name,
-            "xyz.openbmc_project.Sensor.Threshold.Warning");
-    }
-    if (thresholds::hasCriticalInterface(thresholds))
-    {
-        thresholdInterfaceCritical = objectServer.add_interface(
-            "/xyz/openbmc_project/sensors/temperature/" + name,
-            "xyz.openbmc_project.Sensor.Threshold.Critical");
+        std::string interface = thresholds::getInterface(threshold.level);
+        thresholdInterfaces[static_cast<size_t>(threshold.level)] =
+            objectServer.add_interface(
+                "/xyz/openbmc_project/sensors/temperature/" + name, interface);
     }
     association = objectServer.add_interface(
         "/xyz/openbmc_project/sensors/temperature/" + name,
@@ -85,8 +80,10 @@
 MCUTempSensor::~MCUTempSensor()
 {
     waitTimer.cancel();
-    objectServer.remove_interface(thresholdInterfaceWarning);
-    objectServer.remove_interface(thresholdInterfaceCritical);
+    for (const auto& iface : thresholdInterfaces)
+    {
+        objectServer.remove_interface(iface);
+    }
     objectServer.remove_interface(sensorInterface);
     objectServer.remove_interface(association);
 }
diff --git a/src/NVMeSensor.cpp b/src/NVMeSensor.cpp
index cdaa28b..3af9f69 100644
--- a/src/NVMeSensor.cpp
+++ b/src/NVMeSensor.cpp
@@ -37,17 +37,12 @@
         "/xyz/openbmc_project/sensors/temperature/" + name,
         "xyz.openbmc_project.Sensor.Value");
 
-    if (thresholds::hasWarningInterface(thresholds))
+    for (const auto& threshold : thresholds)
     {
-        thresholdInterfaceWarning = objectServer.add_interface(
-            "/xyz/openbmc_project/sensors/temperature/" + name,
-            "xyz.openbmc_project.Sensor.Threshold.Warning");
-    }
-    if (thresholds::hasCriticalInterface(thresholds))
-    {
-        thresholdInterfaceCritical = objectServer.add_interface(
-            "/xyz/openbmc_project/sensors/temperature/" + name,
-            "xyz.openbmc_project.Sensor.Threshold.Critical");
+        std::string interface = thresholds::getInterface(threshold.level);
+        thresholdInterfaces[static_cast<size_t>(threshold.level)] =
+            objectServer.add_interface(
+                "/xyz/openbmc_project/sensors/temperature/" + name, interface);
     }
     association = objectServer.add_interface(
         "/xyz/openbmc_project/sensors/temperature/" + name,
@@ -59,8 +54,10 @@
 NVMeSensor::~NVMeSensor()
 {
     // close the input dev to cancel async operations
-    objServer.remove_interface(thresholdInterfaceWarning);
-    objServer.remove_interface(thresholdInterfaceCritical);
+    for (const auto& iface : thresholdInterfaces)
+    {
+        objServer.remove_interface(iface);
+    }
     objServer.remove_interface(sensorInterface);
     objServer.remove_interface(association);
 }
diff --git a/src/PSUSensor.cpp b/src/PSUSensor.cpp
index 4e0d32b..c405ef5 100644
--- a/src/PSUSensor.cpp
+++ b/src/PSUSensor.cpp
@@ -78,15 +78,11 @@
     sensorInterface = objectServer.add_interface(
         dbusPath, "xyz.openbmc_project.Sensor.Value");
 
-    if (thresholds::hasWarningInterface(thresholds))
+    for (const auto& threshold : thresholds)
     {
-        thresholdInterfaceWarning = objectServer.add_interface(
-            dbusPath, "xyz.openbmc_project.Sensor.Threshold.Warning");
-    }
-    if (thresholds::hasCriticalInterface(thresholds))
-    {
-        thresholdInterfaceCritical = objectServer.add_interface(
-            dbusPath, "xyz.openbmc_project.Sensor.Threshold.Critical");
+        std::string interface = thresholds::getInterface(threshold.level);
+        thresholdInterfaces[static_cast<size_t>(threshold.level)] =
+            objectServer.add_interface(dbusPath, interface);
     }
 
     // This should be called before initializing association.
@@ -111,8 +107,10 @@
     waitTimer.cancel();
     inputDev.close();
     objServer.remove_interface(sensorInterface);
-    objServer.remove_interface(thresholdInterfaceWarning);
-    objServer.remove_interface(thresholdInterfaceCritical);
+    for (const auto& iface : thresholdInterfaces)
+    {
+        objServer.remove_interface(iface);
+    }
     objServer.remove_interface(association);
 }
 
diff --git a/src/TachSensor.cpp b/src/TachSensor.cpp
index 2c48cc9..4ba7777 100644
--- a/src/TachSensor.cpp
+++ b/src/TachSensor.cpp
@@ -61,17 +61,12 @@
         "/xyz/openbmc_project/sensors/fan_tach/" + name,
         "xyz.openbmc_project.Sensor.Value");
 
-    if (thresholds::hasWarningInterface(thresholds))
+    for (const auto& threshold : thresholds)
     {
-        thresholdInterfaceWarning = objectServer.add_interface(
-            "/xyz/openbmc_project/sensors/fan_tach/" + name,
-            "xyz.openbmc_project.Sensor.Threshold.Warning");
-    }
-    if (thresholds::hasCriticalInterface(thresholds))
-    {
-        thresholdInterfaceCritical = objectServer.add_interface(
-            "/xyz/openbmc_project/sensors/fan_tach/" + name,
-            "xyz.openbmc_project.Sensor.Threshold.Critical");
+        std::string interface = thresholds::getInterface(threshold.level);
+        thresholdInterfaces[static_cast<size_t>(threshold.level)] =
+            objectServer.add_interface(
+                "/xyz/openbmc_project/sensors/fan_tach/" + name, interface);
     }
     association = objectServer.add_interface(
         "/xyz/openbmc_project/sensors/fan_tach/" + name,
@@ -104,8 +99,10 @@
     // close the input dev to cancel async operations
     inputDev.close();
     waitTimer.cancel();
-    objServer.remove_interface(thresholdInterfaceWarning);
-    objServer.remove_interface(thresholdInterfaceCritical);
+    for (const auto& iface : thresholdInterfaces)
+    {
+        objServer.remove_interface(iface);
+    }
     objServer.remove_interface(sensorInterface);
     objServer.remove_interface(association);
     objServer.remove_interface(itemIface);
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