xref: /openbmc/phosphor-power/phosphor-regulators/src/actions/if_action.hpp (revision 6d5977375f5ca0a8676c6337e465bf7c92f3a765)
1 /**
2  * Copyright © 2019 IBM Corporation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 #pragma once
17 
18 #include "action.hpp"
19 #include "action_environment.hpp"
20 
21 #include <memory>
22 #include <utility>
23 #include <vector>
24 
25 namespace phosphor::power::regulators
26 {
27 
28 /**
29  * @class IfAction
30  *
31  * Performs actions based on whether a condition is true.
32  *
33  * Implements the "if" action in the JSON config file.  The "if" action provides
34  * a standard if/then/else structure within the JSON config file.
35  *
36  * The "if" action contains three parts:
37  *   - condition
38  *   - then clause
39  *   - else clause (optional)
40  *
41  * The condition is a single action.  The action is executed to determine if the
42  * condition is true.
43  *
44  * If the condition is true, the actions in the "then" clause are executed.
45  *
46  * If the condition is false, the actions in the "else" clause are executed (if
47  * specified).
48  */
49 class IfAction : public Action
50 {
51   public:
52     // Specify which compiler-generated methods we want
53     IfAction() = delete;
54     IfAction(const IfAction&) = delete;
55     IfAction(IfAction&&) = delete;
56     IfAction& operator=(const IfAction&) = delete;
57     IfAction& operator=(IfAction&&) = delete;
58     virtual ~IfAction() = default;
59 
60     /**
61      * Constructor.
62      *
63      * @param conditionAction action that tests whether condition is true
64      * @param thenActions actions to perform if condition is true
65      * @param elseActions actions to perform if condition is false (optional)
66      */
67     explicit IfAction(std::unique_ptr<Action> conditionAction,
68                       std::vector<std::unique_ptr<Action>> thenActions,
69                       std::vector<std::unique_ptr<Action>> elseActions =
70                           std::vector<std::unique_ptr<Action>>{}) :
71         conditionAction{std::move(conditionAction)},
72         thenActions{std::move(thenActions)}, elseActions{std::move(elseActions)}
73     {
74     }
75 
76     /**
77      * Executes the condition action specified in the constructor.
78      *
79      * If the condition action returns true, the actions in the "then" clause
80      * will be executed.  Returns the return value of the last action in the
81      * "then" clause.
82      *
83      * If the condition action returns false, the actions in the "else" clause
84      * will be executed.  Returns the return value of the last action in the
85      * "else" clause.  If no "else" clause was specified, returns false.
86      *
87      * Throws an exception if an error occurs and an action cannot be
88      * successfully executed.
89      *
90      * @param environment action execution environment
91      * @return return value from last action in "then" or "else" clause
92      */
93     virtual bool execute(ActionEnvironment& environment) override;
94 
95     /**
96      * Returns the action that tests whether the condition is true.
97      *
98      * @return condition action
99      */
100     const std::unique_ptr<Action>& getConditionAction() const
101     {
102         return conditionAction;
103     }
104 
105     /**
106      * Returns the actions in the "then" clause.
107      *
108      * These actions are executed if the condition is true.
109      *
110      * @return then clause actions
111      */
112     const std::vector<std::unique_ptr<Action>>& getThenActions() const
113     {
114         return thenActions;
115     }
116 
117     /**
118      * Returns the actions in the "else" clause.
119      *
120      * These actions are executed if the condition is false.
121      *
122      * @return else clause actions
123      */
124     const std::vector<std::unique_ptr<Action>>& getElseActions() const
125     {
126         return elseActions;
127     }
128 
129   private:
130     /**
131      * Action that tests whether the condition is true.
132      */
133     std::unique_ptr<Action> conditionAction{};
134 
135     /**
136      * Actions in the "then" clause.  Executed if condition is true.
137      */
138     std::vector<std::unique_ptr<Action>> thenActions{};
139 
140     /**
141      * Actions in the "else" clause.  Executed if condition is false.  Optional.
142      */
143     std::vector<std::unique_ptr<Action>> elseActions{};
144 };
145 
146 } // namespace phosphor::power::regulators
147