add create_threshold function

Move threshold logic into a seperate function for later changes.

Change-Id: I9dadb014ee42fc19ba686413dfa2c0da70f2582f
Signed-off-by: Rashmica Gupta <rashmica.g@gmail.com>
diff --git a/virtualSensor.cpp b/virtualSensor.cpp
index 1e1d5bb..8b2792a 100644
--- a/virtualSensor.cpp
+++ b/virtualSensor.cpp
@@ -98,73 +98,8 @@
 
     /* Get threshold values if defined in config */
     auto threshold = sensorConfig.value("Threshold", empty);
-    if (!threshold.empty())
-    {
-        // Only create the threshold interfaces if
-        // at least one of their values is present.
 
-        if (threshold.contains("CriticalHigh") ||
-            threshold.contains("CriticalLow"))
-        {
-            criticalIface = std::make_unique<Threshold<CriticalObject>>(
-                bus, objPath.c_str());
-
-            criticalIface->criticalHigh(threshold.value(
-                "CriticalHigh", std::numeric_limits<double>::quiet_NaN()));
-            criticalIface->criticalLow(threshold.value(
-                "CriticalLow", std::numeric_limits<double>::quiet_NaN()));
-        }
-
-        if (threshold.contains("WarningHigh") ||
-            threshold.contains("WarningLow"))
-        {
-            warningIface = std::make_unique<Threshold<WarningObject>>(
-                bus, objPath.c_str());
-
-            warningIface->warningHigh(threshold.value(
-                "WarningHigh", std::numeric_limits<double>::quiet_NaN()));
-            warningIface->warningLow(threshold.value(
-                "WarningLow", std::numeric_limits<double>::quiet_NaN()));
-        }
-
-        if (threshold.contains("HardShutdownHigh") ||
-            threshold.contains("HardShutdownLow"))
-        {
-            hardShutdownIface = std::make_unique<Threshold<HardShutdownObject>>(
-                bus, objPath.c_str());
-
-            hardShutdownIface->hardShutdownHigh(threshold.value(
-                "HardShutdownHigh", std::numeric_limits<double>::quiet_NaN()));
-            hardShutdownIface->hardShutdownLow(threshold.value(
-                "HardShutdownLow", std::numeric_limits<double>::quiet_NaN()));
-        }
-
-        if (threshold.contains("SoftShutdownHigh") ||
-            threshold.contains("SoftShutdownLow"))
-        {
-            softShutdownIface = std::make_unique<Threshold<SoftShutdownObject>>(
-                bus, objPath.c_str());
-
-            softShutdownIface->softShutdownHigh(threshold.value(
-                "SoftShutdownHigh", std::numeric_limits<double>::quiet_NaN()));
-            softShutdownIface->softShutdownLow(threshold.value(
-                "SoftShutdownLow", std::numeric_limits<double>::quiet_NaN()));
-        }
-
-        if (threshold.contains("PerformanceLossHigh") ||
-            threshold.contains("PerformanceLossLow"))
-        {
-            perfLossIface = std::make_unique<Threshold<PerformanceLossObject>>(
-                bus, objPath.c_str());
-
-            perfLossIface->performanceLossHigh(
-                threshold.value("PerformanceLossHigh",
-                                std::numeric_limits<double>::quiet_NaN()));
-            perfLossIface->performanceLossLow(
-                threshold.value("PerformanceLossLow",
-                                std::numeric_limits<double>::quiet_NaN()));
-        }
-    }
+    createThresholds(threshold, objPath);
 
     /* Get MaxValue, MinValue setting if defined in config */
     auto confDesc = sensorConfig.value("Desc", empty);
@@ -314,6 +249,74 @@
     checkThresholds(val, hardShutdownIface);
 }
 
+void VirtualSensor::createThresholds(const Json& threshold,
+                                     const std::string& objPath)
+{
+    if (threshold.empty())
+    {
+        return;
+    }
+    // Only create the threshold interfaces if
+    // at least one of their values is present.
+    if (threshold.contains("CriticalHigh") || threshold.contains("CriticalLow"))
+    {
+        criticalIface =
+            std::make_unique<Threshold<CriticalObject>>(bus, objPath.c_str());
+
+        criticalIface->criticalHigh(threshold.value(
+            "CriticalHigh", std::numeric_limits<double>::quiet_NaN()));
+        criticalIface->criticalLow(threshold.value(
+            "CriticalLow", std::numeric_limits<double>::quiet_NaN()));
+    }
+
+    if (threshold.contains("WarningHigh") || threshold.contains("WarningLow"))
+    {
+        warningIface =
+            std::make_unique<Threshold<WarningObject>>(bus, objPath.c_str());
+
+        warningIface->warningHigh(threshold.value(
+            "WarningHigh", std::numeric_limits<double>::quiet_NaN()));
+        warningIface->warningLow(threshold.value(
+            "WarningLow", std::numeric_limits<double>::quiet_NaN()));
+    }
+
+    if (threshold.contains("HardShutdownHigh") ||
+        threshold.contains("HardShutdownLow"))
+    {
+        hardShutdownIface = std::make_unique<Threshold<HardShutdownObject>>(
+            bus, objPath.c_str());
+
+        hardShutdownIface->hardShutdownHigh(threshold.value(
+            "HardShutdownHigh", std::numeric_limits<double>::quiet_NaN()));
+        hardShutdownIface->hardShutdownLow(threshold.value(
+            "HardShutdownLow", std::numeric_limits<double>::quiet_NaN()));
+    }
+
+    if (threshold.contains("SoftShutdownHigh") ||
+        threshold.contains("SoftShutdownLow"))
+    {
+        softShutdownIface = std::make_unique<Threshold<SoftShutdownObject>>(
+            bus, objPath.c_str());
+
+        softShutdownIface->softShutdownHigh(threshold.value(
+            "SoftShutdownHigh", std::numeric_limits<double>::quiet_NaN()));
+        softShutdownIface->softShutdownLow(threshold.value(
+            "SoftShutdownLow", std::numeric_limits<double>::quiet_NaN()));
+    }
+
+    if (threshold.contains("PerformanceLossHigh") ||
+        threshold.contains("PerformanceLossLow"))
+    {
+        perfLossIface = std::make_unique<Threshold<PerformanceLossObject>>(
+            bus, objPath.c_str());
+
+        perfLossIface->performanceLossHigh(threshold.value(
+            "PerformanceLossHigh", std::numeric_limits<double>::quiet_NaN()));
+        perfLossIface->performanceLossLow(threshold.value(
+            "PerformanceLossLow", std::numeric_limits<double>::quiet_NaN()));
+    }
+}
+
 /** @brief Parsing Virtual Sensor config JSON file  */
 Json VirtualSensors::parseConfigFile(const std::string configFile)
 {
diff --git a/virtualSensor.hpp b/virtualSensor.hpp
index 71a66ca..6107fa1 100644
--- a/virtualSensor.hpp
+++ b/virtualSensor.hpp
@@ -132,6 +132,9 @@
     void initVirtualSensor(const Json& sensorConfig,
                            const std::string& objPath);
 
+    /** @brief create threshold objects from json config */
+    void createThresholds(const Json& threshold, const std::string& objPath);
+
     /** @brief Check Sensor threshold and update alarm and log */
     template <typename V, typename T>
     void checkThresholds(V value, T& threshold)