Create regulators RunRuleAction class

Create the RunRuleAction class that implements the run_rule action in
the JSON config file.

Signed-off-by: Shawn McCarney <shawnmm@us.ibm.com>
Change-Id: I382d072f6648f7609b0eb2b4e8a51b7fb18d54db
diff --git a/phosphor-regulators/src/actions/action_environment.hpp b/phosphor-regulators/src/actions/action_environment.hpp
index eb66981..ff6259a 100644
--- a/phosphor-regulators/src/actions/action_environment.hpp
+++ b/phosphor-regulators/src/actions/action_environment.hpp
@@ -165,12 +165,15 @@
      * Throws runtime_error if the new depth exceeds maxRuleDepth.  This
      * indicates that infinite recursion has probably occurred (rule A -> rule B
      * -> rule A).
+     *
+     * @param ruleID ID of the rule that is being called
      */
-    void incrementRuleDepth()
+    void incrementRuleDepth(const std::string& ruleID)
     {
         if (ruleDepth >= maxRuleDepth)
         {
-            throw std::runtime_error("Maximum rule depth exceeded.");
+            throw std::runtime_error("Maximum rule depth exceeded by rule " +
+                                     ruleID + '.');
         }
         ++ruleDepth;
     }
diff --git a/phosphor-regulators/src/actions/run_rule_action.hpp b/phosphor-regulators/src/actions/run_rule_action.hpp
new file mode 100644
index 0000000..8e84a89
--- /dev/null
+++ b/phosphor-regulators/src/actions/run_rule_action.hpp
@@ -0,0 +1,99 @@
+/**
+ * Copyright © 2019 IBM Corporation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+#pragma once
+
+#include "action.hpp"
+#include "action_environment.hpp"
+#include "rule.hpp"
+
+#include <string>
+
+namespace phosphor::power::regulators
+{
+
+/**
+ * @class RunRuleAction
+ *
+ * Runs the specified rule.
+ *
+ * Implements the run_rule action in the JSON config file.
+ */
+class RunRuleAction : public Action
+{
+  public:
+    // Specify which compiler-generated methods we want
+    RunRuleAction() = delete;
+    RunRuleAction(const RunRuleAction&) = delete;
+    RunRuleAction(RunRuleAction&&) = delete;
+    RunRuleAction& operator=(const RunRuleAction&) = delete;
+    RunRuleAction& operator=(RunRuleAction&&) = delete;
+    virtual ~RunRuleAction() = default;
+
+    /**
+     * Constructor.
+     *
+     * @param ruleID rule ID
+     */
+    explicit RunRuleAction(const std::string& ruleID) : ruleID{ruleID}
+    {
+    }
+
+    /**
+     * Executes this action.
+     *
+     * Runs the rule specified in the constructor.
+     *
+     * Returns the return value from the last action in the rule.
+     *
+     * Throws an exception if an error occurs and an action cannot be
+     * successfully executed.
+     *
+     * @param environment action execution environment
+     * @return return value from last action in rule
+     */
+    virtual bool execute(ActionEnvironment& environment) override
+    {
+        // Increment rule call stack depth since we are running a rule.  Rule
+        // depth is used to detect infinite recursion.
+        environment.incrementRuleDepth(ruleID);
+
+        // Execute rule
+        bool returnValue = environment.getRule(ruleID).execute(environment);
+
+        // Decrement rule depth since rule has returned
+        environment.decrementRuleDepth();
+
+        return returnValue;
+    }
+
+    /**
+     * Returns the rule ID.
+     *
+     * @return rule ID
+     */
+    const std::string& getRuleID() const
+    {
+        return ruleID;
+    }
+
+  private:
+    /**
+     * Rule ID.
+     */
+    const std::string ruleID{};
+};
+
+} // namespace phosphor::power::regulators
diff --git a/phosphor-regulators/test/actions/action_environment_tests.cpp b/phosphor-regulators/test/actions/action_environment_tests.cpp
index ff13c83..d861a25 100644
--- a/phosphor-regulators/test/actions/action_environment_tests.cpp
+++ b/phosphor-regulators/test/actions/action_environment_tests.cpp
@@ -20,7 +20,9 @@
 
 #include <cstddef> // for size_t
 #include <exception>
+#include <memory>
 #include <stdexcept>
+#include <vector>
 
 #include <gtest/gtest.h>
 
@@ -54,8 +56,8 @@
     IDMap idMap{};
     ActionEnvironment env{idMap, ""};
     EXPECT_EQ(env.getRuleDepth(), 0);
-    env.incrementRuleDepth();
-    env.incrementRuleDepth();
+    env.incrementRuleDepth("set_voltage_rule");
+    env.incrementRuleDepth("set_voltage_rule");
     EXPECT_EQ(env.getRuleDepth(), 2);
     env.decrementRuleDepth();
     EXPECT_EQ(env.getRuleDepth(), 1);
@@ -157,9 +159,9 @@
     IDMap idMap{};
     ActionEnvironment env{idMap, ""};
     EXPECT_EQ(env.getRuleDepth(), 0);
-    env.incrementRuleDepth();
+    env.incrementRuleDepth("set_voltage_rule");
     EXPECT_EQ(env.getRuleDepth(), 1);
-    env.incrementRuleDepth();
+    env.incrementRuleDepth("set_voltage_rule");
     EXPECT_EQ(env.getRuleDepth(), 2);
     env.decrementRuleDepth();
     EXPECT_EQ(env.getRuleDepth(), 1);
@@ -220,7 +222,7 @@
     {
         for (size_t i = 1; i <= env.maxRuleDepth; ++i)
         {
-            env.incrementRuleDepth();
+            env.incrementRuleDepth("set_voltage_rule");
             EXPECT_EQ(env.getRuleDepth(), i);
         }
     }
@@ -232,11 +234,12 @@
     // Test where rule depth has exceeded maximum
     try
     {
-        env.incrementRuleDepth();
+        env.incrementRuleDepth("set_voltage_rule");
     }
     catch (const std::runtime_error& r_error)
     {
-        EXPECT_STREQ(r_error.what(), "Maximum rule depth exceeded.");
+        EXPECT_STREQ(r_error.what(),
+                     "Maximum rule depth exceeded by rule set_voltage_rule.");
     }
     catch (const std::exception& error)
     {
diff --git a/phosphor-regulators/test/actions/run_rule_action_tests.cpp b/phosphor-regulators/test/actions/run_rule_action_tests.cpp
new file mode 100644
index 0000000..be94e52
--- /dev/null
+++ b/phosphor-regulators/test/actions/run_rule_action_tests.cpp
@@ -0,0 +1,189 @@
+/**
+ * Copyright © 2019 IBM Corporation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+#include "action.hpp"
+#include "action_environment.hpp"
+#include "device.hpp"
+#include "id_map.hpp"
+#include "mock_action.hpp"
+#include "rule.hpp"
+#include "run_rule_action.hpp"
+
+#include <exception>
+#include <memory>
+#include <stdexcept>
+#include <utility>
+#include <vector>
+
+#include <gmock/gmock.h>
+#include <gtest/gtest.h>
+
+using namespace phosphor::power::regulators;
+
+using ::testing::Return;
+using ::testing::Throw;
+
+TEST(RunRuleActionTests, Constructor)
+{
+    RunRuleAction action{"set_voltage_rule"};
+    EXPECT_EQ(action.getRuleID(), "set_voltage_rule");
+}
+
+TEST(RunRuleActionTests, Execute)
+{
+    // Test where rule ID is not in the IDMap/ActionEnvironment
+    try
+    {
+        IDMap idMap{};
+        ActionEnvironment env{idMap, ""};
+        RunRuleAction runRuleAction{"set_voltage_rule"};
+        runRuleAction.execute(env);
+        ADD_FAILURE() << "Should not have reached this line.";
+    }
+    catch (const std::invalid_argument& ia_error)
+    {
+        EXPECT_STREQ(ia_error.what(),
+                     "Unable to find rule with ID \"set_voltage_rule\"");
+    }
+    catch (const std::exception& error)
+    {
+        ADD_FAILURE() << "Should not have caught exception.";
+    }
+
+    // Test where a rule action throws an exception
+    try
+    {
+        // Create rule with action that throws an exception
+        std::vector<std::unique_ptr<Action>> actions{};
+        std::unique_ptr<MockAction> action = std::make_unique<MockAction>();
+        EXPECT_CALL(*action, execute)
+            .Times(1)
+            .WillOnce(Throw(std::logic_error{"Communication error"}));
+        actions.push_back(std::move(action));
+        Rule rule("exception_rule", std::move(actions));
+
+        // Create ActionEnvironment
+        IDMap idMap{};
+        idMap.addRule(rule);
+        ActionEnvironment env{idMap, ""};
+
+        // Create RunRuleAction
+        RunRuleAction runRuleAction{"exception_rule"};
+        runRuleAction.execute(env);
+        ADD_FAILURE() << "Should not have reached this line.";
+    }
+    catch (const std::exception& error)
+    {
+        EXPECT_STREQ(error.what(), "Communication error");
+    }
+
+    // Test where rule calls itself and results in infinite recursion
+    try
+    {
+        // Create rule that calls itself
+        std::vector<std::unique_ptr<Action>> actions{};
+        actions.push_back(std::make_unique<RunRuleAction>("infinite_rule"));
+        Rule rule("infinite_rule", std::move(actions));
+
+        // Create ActionEnvironment
+        IDMap idMap{};
+        idMap.addRule(rule);
+        ActionEnvironment env{idMap, ""};
+
+        // Create RunRuleAction
+        RunRuleAction runRuleAction{"infinite_rule"};
+        runRuleAction.execute(env);
+        ADD_FAILURE() << "Should not have reached this line.";
+    }
+    catch (const std::runtime_error& r_error)
+    {
+        EXPECT_STREQ(r_error.what(),
+                     "Maximum rule depth exceeded by rule infinite_rule.");
+    }
+    catch (const std::exception& error)
+    {
+        ADD_FAILURE() << "Should not have caught exception.";
+    }
+
+    // Test where last action returns false
+    try
+    {
+        // Create rule with two actions.  Last action returns false.
+        std::vector<std::unique_ptr<Action>> actions{};
+        std::unique_ptr<MockAction> action;
+
+        action = std::make_unique<MockAction>();
+        EXPECT_CALL(*action, execute).Times(1).WillOnce(Return(true));
+        actions.push_back(std::move(action));
+
+        action = std::make_unique<MockAction>();
+        EXPECT_CALL(*action, execute).Times(1).WillOnce(Return(false));
+        actions.push_back(std::move(action));
+
+        Rule rule("set_voltage_rule", std::move(actions));
+
+        // Create ActionEnvironment
+        IDMap idMap{};
+        idMap.addRule(rule);
+        ActionEnvironment env{idMap, ""};
+
+        // Create RunRuleAction
+        RunRuleAction runRuleAction{"set_voltage_rule"};
+        EXPECT_EQ(runRuleAction.execute(env), false);
+        EXPECT_EQ(env.getRuleDepth(), 0);
+    }
+    catch (const std::exception& error)
+    {
+        ADD_FAILURE() << "Should not have caught exception.";
+    }
+
+    // Test where last action returns true
+    try
+    {
+        // Create rule with two actions.  Last action returns true.
+        std::vector<std::unique_ptr<Action>> actions{};
+        std::unique_ptr<MockAction> action;
+
+        action = std::make_unique<MockAction>();
+        EXPECT_CALL(*action, execute).Times(1).WillOnce(Return(false));
+        actions.push_back(std::move(action));
+
+        action = std::make_unique<MockAction>();
+        EXPECT_CALL(*action, execute).Times(1).WillOnce(Return(true));
+        actions.push_back(std::move(action));
+
+        Rule rule("set_voltage_rule", std::move(actions));
+
+        // Create ActionEnvironment
+        IDMap idMap{};
+        idMap.addRule(rule);
+        ActionEnvironment env{idMap, ""};
+
+        // Create RunRuleAction
+        RunRuleAction runRuleAction{"set_voltage_rule"};
+        EXPECT_EQ(runRuleAction.execute(env), true);
+        EXPECT_EQ(env.getRuleDepth(), 0);
+    }
+    catch (const std::exception& error)
+    {
+        ADD_FAILURE() << "Should not have caught exception.";
+    }
+}
+
+TEST(RunRuleActionTests, GetRuleID)
+{
+    RunRuleAction action{"read_sensors_rule"};
+    EXPECT_EQ(action.getRuleID(), "read_sensors_rule");
+}
diff --git a/phosphor-regulators/test/id_map_tests.cpp b/phosphor-regulators/test/id_map_tests.cpp
index fae5fff..ffa6c15 100644
--- a/phosphor-regulators/test/id_map_tests.cpp
+++ b/phosphor-regulators/test/id_map_tests.cpp
@@ -19,8 +19,10 @@
 #include "rule.hpp"
 
 #include <exception>
+#include <memory>
 #include <stdexcept>
 #include <string>
+#include <vector>
 
 #include <gtest/gtest.h>
 
diff --git a/phosphor-regulators/test/meson.build b/phosphor-regulators/test/meson.build
index a246150..7157fd6 100644
--- a/phosphor-regulators/test/meson.build
+++ b/phosphor-regulators/test/meson.build
@@ -11,6 +11,7 @@
 
     'actions/action_environment_tests.cpp',
     'actions/action_utils_tests.cpp',
+    'actions/run_rule_action_tests.cpp',
     'actions/set_device_action_tests.cpp'
 ]