Create regulators IfAction class
Create the IfAction class that implements the "if" action in the JSON
config file.
See phosphor-regulators/docs/config_file/if.md for more information on
the "if" action.
Signed-off-by: Shawn McCarney <shawnmm@us.ibm.com>
Change-Id: I16d5ba7bb88b5baa8a5085168a8bd2dc75449e9b
diff --git a/phosphor-regulators/src/actions/if_action.cpp b/phosphor-regulators/src/actions/if_action.cpp
new file mode 100644
index 0000000..f21e7da
--- /dev/null
+++ b/phosphor-regulators/src/actions/if_action.cpp
@@ -0,0 +1,52 @@
+/**
+ * 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 "if_action.hpp"
+
+#include "action_utils.hpp"
+
+namespace phosphor::power::regulators
+{
+
+bool IfAction::execute(ActionEnvironment& environment)
+{
+ bool returnValue{true};
+
+ // Execute condition action and check whether it returned true
+ if (conditionAction->execute(environment) == true)
+ {
+ // Condition was true; execute actions in "then" clause
+ returnValue = action_utils::execute(thenActions, environment);
+ }
+ else
+ {
+ // Condition was false; check if optional "else" clause was specified
+ if (elseActions.size() > 0)
+ {
+ // Execute actions in "else" clause
+ returnValue = action_utils::execute(elseActions, environment);
+ }
+ else
+ {
+ // No "else" clause specified; return value is false in this case
+ returnValue = false;
+ }
+ }
+
+ return returnValue;
+}
+
+} // namespace phosphor::power::regulators
diff --git a/phosphor-regulators/src/actions/if_action.hpp b/phosphor-regulators/src/actions/if_action.hpp
new file mode 100644
index 0000000..13b9860
--- /dev/null
+++ b/phosphor-regulators/src/actions/if_action.hpp
@@ -0,0 +1,146 @@
+/**
+ * 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 <memory>
+#include <utility>
+#include <vector>
+
+namespace phosphor::power::regulators
+{
+
+/**
+ * @class IfAction
+ *
+ * Performs actions based on whether a condition is true.
+ *
+ * Implements the "if" action in the JSON config file. The "if" action provides
+ * a standard if/then/else structure within the JSON config file.
+ *
+ * The "if" action contains three parts:
+ * - condition
+ * - then clause
+ * - else clause (optional)
+ *
+ * The condition is a single action. The action is executed to determine if the
+ * condition is true.
+ *
+ * If the condition is true, the actions in the "then" clause are executed.
+ *
+ * If the condition is false, the actions in the "else" clause are executed (if
+ * specified).
+ */
+class IfAction : public Action
+{
+ public:
+ // Specify which compiler-generated methods we want
+ IfAction() = delete;
+ IfAction(const IfAction&) = delete;
+ IfAction(IfAction&&) = delete;
+ IfAction& operator=(const IfAction&) = delete;
+ IfAction& operator=(IfAction&&) = delete;
+ virtual ~IfAction() = default;
+
+ /**
+ * Constructor.
+ *
+ * @param conditionAction action that tests whether condition is true
+ * @param thenActions actions to perform if condition is true
+ * @param elseActions actions to perform if condition is false (optional)
+ */
+ explicit IfAction(std::unique_ptr<Action> conditionAction,
+ std::vector<std::unique_ptr<Action>> thenActions,
+ std::vector<std::unique_ptr<Action>> elseActions =
+ std::vector<std::unique_ptr<Action>>{}) :
+ conditionAction{std::move(conditionAction)},
+ thenActions{std::move(thenActions)}, elseActions{std::move(elseActions)}
+ {
+ }
+
+ /**
+ * Executes the condition action specified in the constructor.
+ *
+ * If the condition action returns true, the actions in the "then" clause
+ * will be executed. Returns the return value of the last action in the
+ * "then" clause.
+ *
+ * If the condition action returns false, the actions in the "else" clause
+ * will be executed. Returns the return value of the last action in the
+ * "else" clause. If no "else" clause was specified, returns false.
+ *
+ * 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 "then" or "else" clause
+ */
+ virtual bool execute(ActionEnvironment& environment) override;
+
+ /**
+ * Returns the action that tests whether the condition is true.
+ *
+ * @return condition action
+ */
+ const std::unique_ptr<Action>& getConditionAction() const
+ {
+ return conditionAction;
+ }
+
+ /**
+ * Returns the actions in the "then" clause.
+ *
+ * These actions are executed if the condition is true.
+ *
+ * @return then clause actions
+ */
+ const std::vector<std::unique_ptr<Action>>& getThenActions() const
+ {
+ return thenActions;
+ }
+
+ /**
+ * Returns the actions in the "else" clause.
+ *
+ * These actions are executed if the condition is false.
+ *
+ * @return else clause actions
+ */
+ const std::vector<std::unique_ptr<Action>>& getElseActions() const
+ {
+ return elseActions;
+ }
+
+ private:
+ /**
+ * Action that tests whether the condition is true.
+ */
+ std::unique_ptr<Action> conditionAction{};
+
+ /**
+ * Actions in the "then" clause. Executed if condition is true.
+ */
+ std::vector<std::unique_ptr<Action>> thenActions{};
+
+ /**
+ * Actions in the "else" clause. Executed if condition is false. Optional.
+ */
+ std::vector<std::unique_ptr<Action>> elseActions{};
+};
+
+} // namespace phosphor::power::regulators
diff --git a/phosphor-regulators/src/meson.build b/phosphor-regulators/src/meson.build
index 76551ae..e93d65f 100644
--- a/phosphor-regulators/src/meson.build
+++ b/phosphor-regulators/src/meson.build
@@ -4,7 +4,9 @@
)
phosphor_regulators_source_files = [
- 'id_map.cpp'
+ 'id_map.cpp',
+
+ 'actions/if_action.cpp'
]
phosphor_regulators_library = static_library(
diff --git a/phosphor-regulators/test/actions/if_action_tests.cpp b/phosphor-regulators/test/actions/if_action_tests.cpp
new file mode 100644
index 0000000..1f67f27
--- /dev/null
+++ b/phosphor-regulators/test/actions/if_action_tests.cpp
@@ -0,0 +1,300 @@
+/**
+ * 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 "id_map.hpp"
+#include "if_action.hpp"
+#include "mock_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(IfActionTests, Constructor)
+{
+ // Test where else clause is not specified
+ {
+ std::unique_ptr<Action> conditionAction =
+ std::make_unique<MockAction>();
+
+ std::vector<std::unique_ptr<Action>> thenActions{};
+ thenActions.push_back(std::make_unique<MockAction>());
+ thenActions.push_back(std::make_unique<MockAction>());
+
+ IfAction ifAction{std::move(conditionAction), std::move(thenActions)};
+ EXPECT_NE(ifAction.getConditionAction().get(), nullptr);
+ EXPECT_EQ(ifAction.getThenActions().size(), 2);
+ EXPECT_EQ(ifAction.getElseActions().size(), 0);
+ }
+
+ // Test where else clause is specified
+ {
+ std::unique_ptr<Action> conditionAction =
+ std::make_unique<MockAction>();
+
+ std::vector<std::unique_ptr<Action>> thenActions{};
+ thenActions.push_back(std::make_unique<MockAction>());
+ thenActions.push_back(std::make_unique<MockAction>());
+
+ std::vector<std::unique_ptr<Action>> elseActions{};
+ elseActions.push_back(std::make_unique<MockAction>());
+
+ IfAction ifAction{std::move(conditionAction), std::move(thenActions),
+ std::move(elseActions)};
+ EXPECT_NE(ifAction.getConditionAction().get(), nullptr);
+ EXPECT_EQ(ifAction.getThenActions().size(), 2);
+ EXPECT_EQ(ifAction.getElseActions().size(), 1);
+ }
+}
+
+TEST(IfActionTests, Execute)
+{
+ // Create ActionEnvironment
+ IDMap idMap{};
+ ActionEnvironment env{idMap, ""};
+
+ // Test where action throws an exception
+ try
+ {
+ // Create condition action that will return true
+ std::unique_ptr<MockAction> conditionAction =
+ std::make_unique<MockAction>();
+ EXPECT_CALL(*conditionAction, execute).Times(1).WillOnce(Return(true));
+
+ // Create vector of actions for then clause
+ std::vector<std::unique_ptr<Action>> thenActions{};
+ std::unique_ptr<MockAction> thenAction;
+
+ // First then action will throw an exception
+ thenAction = std::make_unique<MockAction>();
+ EXPECT_CALL(*thenAction, execute)
+ .Times(1)
+ .WillOnce(Throw(std::logic_error{"Communication error"}));
+ thenActions.push_back(std::move(thenAction));
+
+ // Second then action should not get executed
+ thenAction = std::make_unique<MockAction>();
+ EXPECT_CALL(*thenAction, execute).Times(0);
+ thenActions.push_back(std::move(thenAction));
+
+ IfAction ifAction{std::move(conditionAction), std::move(thenActions)};
+ ifAction.execute(env);
+ ADD_FAILURE() << "Should not have reached this line.";
+ }
+ catch (const std::exception& error)
+ {
+ EXPECT_STREQ(error.what(), "Communication error");
+ }
+
+ // Test where condition is true: then clause returns true
+ try
+ {
+ // Create condition action that will return true
+ std::unique_ptr<MockAction> conditionAction =
+ std::make_unique<MockAction>();
+ EXPECT_CALL(*conditionAction, execute).Times(1).WillOnce(Return(true));
+
+ // Create vector of actions for then clause: last action returns true
+ std::vector<std::unique_ptr<Action>> thenActions{};
+ std::unique_ptr<MockAction> thenAction = std::make_unique<MockAction>();
+ EXPECT_CALL(*thenAction, execute).Times(1).WillOnce(Return(true));
+ thenActions.push_back(std::move(thenAction));
+
+ // Create vector of actions for else clause: should not be executed
+ std::vector<std::unique_ptr<Action>> elseActions{};
+ std::unique_ptr<MockAction> elseAction = std::make_unique<MockAction>();
+ EXPECT_CALL(*elseAction, execute).Times(0);
+ elseActions.push_back(std::move(elseAction));
+
+ IfAction ifAction{std::move(conditionAction), std::move(thenActions),
+ std::move(elseActions)};
+ EXPECT_EQ(ifAction.execute(env), true);
+ }
+ catch (const std::exception& error)
+ {
+ ADD_FAILURE() << "Should not have caught exception.";
+ }
+
+ // Test where condition is true: then clause returns false
+ try
+ {
+ // Create condition action that will return true
+ std::unique_ptr<MockAction> conditionAction =
+ std::make_unique<MockAction>();
+ EXPECT_CALL(*conditionAction, execute).Times(1).WillOnce(Return(true));
+
+ // Create vector of actions for then clause: last action returns false
+ std::vector<std::unique_ptr<Action>> thenActions{};
+ std::unique_ptr<MockAction> thenAction = std::make_unique<MockAction>();
+ EXPECT_CALL(*thenAction, execute).Times(1).WillOnce(Return(false));
+ thenActions.push_back(std::move(thenAction));
+
+ // Create vector of actions for else clause: should not be executed
+ std::vector<std::unique_ptr<Action>> elseActions{};
+ std::unique_ptr<MockAction> elseAction = std::make_unique<MockAction>();
+ EXPECT_CALL(*elseAction, execute).Times(0);
+ elseActions.push_back(std::move(elseAction));
+
+ IfAction ifAction{std::move(conditionAction), std::move(thenActions),
+ std::move(elseActions)};
+ EXPECT_EQ(ifAction.execute(env), false);
+ }
+ catch (const std::exception& error)
+ {
+ ADD_FAILURE() << "Should not have caught exception.";
+ }
+
+ // Test where condition is false: else clause returns true
+ try
+ {
+ // Create condition action that will return false
+ std::unique_ptr<MockAction> conditionAction =
+ std::make_unique<MockAction>();
+ EXPECT_CALL(*conditionAction, execute).Times(1).WillOnce(Return(false));
+
+ // Create vector of actions for then clause: should not be executed
+ std::vector<std::unique_ptr<Action>> thenActions{};
+ std::unique_ptr<MockAction> thenAction = std::make_unique<MockAction>();
+ EXPECT_CALL(*thenAction, execute).Times(0);
+ thenActions.push_back(std::move(thenAction));
+
+ // Create vector of actions for else clause: last action returns true
+ std::vector<std::unique_ptr<Action>> elseActions{};
+ std::unique_ptr<MockAction> elseAction = std::make_unique<MockAction>();
+ EXPECT_CALL(*elseAction, execute).Times(1).WillOnce(Return(true));
+ elseActions.push_back(std::move(elseAction));
+
+ IfAction ifAction{std::move(conditionAction), std::move(thenActions),
+ std::move(elseActions)};
+ EXPECT_EQ(ifAction.execute(env), true);
+ }
+ catch (const std::exception& error)
+ {
+ ADD_FAILURE() << "Should not have caught exception.";
+ }
+
+ // Test where condition is false: else clause returns false
+ try
+ {
+ // Create condition action that will return false
+ std::unique_ptr<MockAction> conditionAction =
+ std::make_unique<MockAction>();
+ EXPECT_CALL(*conditionAction, execute).Times(1).WillOnce(Return(false));
+
+ // Create vector of actions for then clause: should not be executed
+ std::vector<std::unique_ptr<Action>> thenActions{};
+ std::unique_ptr<MockAction> thenAction = std::make_unique<MockAction>();
+ EXPECT_CALL(*thenAction, execute).Times(0);
+ thenActions.push_back(std::move(thenAction));
+
+ // Create vector of actions for else clause: last action returns false
+ std::vector<std::unique_ptr<Action>> elseActions{};
+ std::unique_ptr<MockAction> elseAction = std::make_unique<MockAction>();
+ EXPECT_CALL(*elseAction, execute).Times(1).WillOnce(Return(false));
+ elseActions.push_back(std::move(elseAction));
+
+ IfAction ifAction{std::move(conditionAction), std::move(thenActions),
+ std::move(elseActions)};
+ EXPECT_EQ(ifAction.execute(env), false);
+ }
+ catch (const std::exception& error)
+ {
+ ADD_FAILURE() << "Should not have caught exception.";
+ }
+
+ // Test where condition is false: no else clause specified
+ try
+ {
+ // Create condition action that will return false
+ std::unique_ptr<MockAction> conditionAction =
+ std::make_unique<MockAction>();
+ EXPECT_CALL(*conditionAction, execute).Times(1).WillOnce(Return(false));
+
+ // Create vector of actions for then clause: should not be executed
+ std::vector<std::unique_ptr<Action>> thenActions{};
+ std::unique_ptr<MockAction> thenAction = std::make_unique<MockAction>();
+ EXPECT_CALL(*thenAction, execute).Times(0);
+ thenActions.push_back(std::move(thenAction));
+
+ IfAction ifAction{std::move(conditionAction), std::move(thenActions)};
+ EXPECT_EQ(ifAction.execute(env), false);
+ }
+ catch (const std::exception& error)
+ {
+ ADD_FAILURE() << "Should not have caught exception.";
+ }
+}
+
+TEST(IfActionTests, GetConditionAction)
+{
+ MockAction* conditionAction = new MockAction{};
+
+ std::vector<std::unique_ptr<Action>> thenActions{};
+
+ IfAction ifAction{std::unique_ptr<Action>{conditionAction},
+ std::move(thenActions)};
+
+ EXPECT_EQ(ifAction.getConditionAction().get(), conditionAction);
+}
+
+TEST(IfActionTests, GetThenActions)
+{
+ std::unique_ptr<Action> conditionAction = std::make_unique<MockAction>();
+
+ std::vector<std::unique_ptr<Action>> thenActions{};
+
+ MockAction* thenAction1 = new MockAction{};
+ thenActions.push_back(std::unique_ptr<MockAction>{thenAction1});
+
+ MockAction* thenAction2 = new MockAction{};
+ thenActions.push_back(std::unique_ptr<MockAction>{thenAction2});
+
+ IfAction ifAction{std::move(conditionAction), std::move(thenActions)};
+ EXPECT_EQ(ifAction.getThenActions().size(), 2);
+ EXPECT_EQ(ifAction.getThenActions()[0].get(), thenAction1);
+ EXPECT_EQ(ifAction.getThenActions()[1].get(), thenAction2);
+}
+
+TEST(IfActionTests, GetElseActions)
+{
+ std::unique_ptr<Action> conditionAction = std::make_unique<MockAction>();
+
+ std::vector<std::unique_ptr<Action>> thenActions{};
+
+ std::vector<std::unique_ptr<Action>> elseActions{};
+
+ MockAction* elseAction1 = new MockAction{};
+ elseActions.push_back(std::unique_ptr<MockAction>{elseAction1});
+
+ MockAction* elseAction2 = new MockAction{};
+ elseActions.push_back(std::unique_ptr<MockAction>{elseAction2});
+
+ IfAction ifAction{std::move(conditionAction), std::move(thenActions),
+ std::move(elseActions)};
+ EXPECT_EQ(ifAction.getElseActions().size(), 2);
+ EXPECT_EQ(ifAction.getElseActions()[0].get(), elseAction1);
+ EXPECT_EQ(ifAction.getElseActions()[1].get(), elseAction2);
+}
diff --git a/phosphor-regulators/test/meson.build b/phosphor-regulators/test/meson.build
index 5a9eea0..1d261f7 100644
--- a/phosphor-regulators/test/meson.build
+++ b/phosphor-regulators/test/meson.build
@@ -12,6 +12,7 @@
'actions/action_environment_tests.cpp',
'actions/action_utils_tests.cpp',
'actions/and_action_tests.cpp',
+ 'actions/if_action_tests.cpp',
'actions/not_action_tests.cpp',
'actions/or_action_tests.cpp',
'actions/run_rule_action_tests.cpp',