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 #include "rule.hpp"
21 
22 #include <string>
23 
24 namespace phosphor::power::regulators
25 {
26 
27 /**
28  * @class RunRuleAction
29  *
30  * Runs the specified rule.
31  *
32  * Implements the run_rule action in the JSON config file.
33  */
34 class RunRuleAction : public Action
35 {
36   public:
37     // Specify which compiler-generated methods we want
38     RunRuleAction() = delete;
39     RunRuleAction(const RunRuleAction&) = delete;
40     RunRuleAction(RunRuleAction&&) = delete;
41     RunRuleAction& operator=(const RunRuleAction&) = delete;
42     RunRuleAction& operator=(RunRuleAction&&) = delete;
43     virtual ~RunRuleAction() = default;
44 
45     /**
46      * Constructor.
47      *
48      * @param ruleID rule ID
49      */
50     explicit RunRuleAction(const std::string& ruleID) : ruleID{ruleID} {}
51 
52     /**
53      * Executes this action.
54      *
55      * Runs the rule specified in the constructor.
56      *
57      * Returns the return value from the last action in the rule.
58      *
59      * Throws an exception if an error occurs and an action cannot be
60      * successfully executed.
61      *
62      * @param environment action execution environment
63      * @return return value from last action in rule
64      */
65     virtual bool execute(ActionEnvironment& environment) override
66     {
67         // Increment rule call stack depth since we are running a rule.  Rule
68         // depth is used to detect infinite recursion.
69         environment.incrementRuleDepth(ruleID);
70 
71         // Execute rule
72         bool returnValue = environment.getRule(ruleID).execute(environment);
73 
74         // Decrement rule depth since rule has returned
75         environment.decrementRuleDepth();
76 
77         return returnValue;
78     }
79 
80     /**
81      * Returns the rule ID.
82      *
83      * @return rule ID
84      */
85     const std::string& getRuleID() const
86     {
87         return ruleID;
88     }
89 
90     /**
91      * Returns a string description of this action.
92      *
93      * @return description of action
94      */
95     virtual std::string toString() const override
96     {
97         return "run_rule: " + ruleID;
98     }
99 
100   private:
101     /**
102      * Rule ID.
103      */
104     const std::string ruleID{};
105 };
106 
107 } // namespace phosphor::power::regulators
108