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 
54     /**
55      * Executes this action.
56      *
57      * Runs the rule specified in the constructor.
58      *
59      * Returns the return value from the last action in the rule.
60      *
61      * Throws an exception if an error occurs and an action cannot be
62      * successfully executed.
63      *
64      * @param environment action execution environment
65      * @return return value from last action in rule
66      */
67     virtual bool execute(ActionEnvironment& environment) override
68     {
69         // Increment rule call stack depth since we are running a rule.  Rule
70         // depth is used to detect infinite recursion.
71         environment.incrementRuleDepth(ruleID);
72 
73         // Execute rule
74         bool returnValue = environment.getRule(ruleID).execute(environment);
75 
76         // Decrement rule depth since rule has returned
77         environment.decrementRuleDepth();
78 
79         return returnValue;
80     }
81 
82     /**
83      * Returns the rule ID.
84      *
85      * @return rule ID
86      */
87     const std::string& getRuleID() const
88     {
89         return ruleID;
90     }
91 
92     /**
93      * Returns a string description of this action.
94      *
95      * @return description of action
96      */
97     virtual std::string toString() const override
98     {
99         return "run_rule: " + ruleID;
100     }
101 
102   private:
103     /**
104      * Rule ID.
105      */
106     const std::string ruleID{};
107 };
108 
109 } // namespace phosphor::power::regulators
110