16d597737SShawn McCarney /** 26d597737SShawn McCarney * Copyright © 2019 IBM Corporation 36d597737SShawn McCarney * 46d597737SShawn McCarney * Licensed under the Apache License, Version 2.0 (the "License"); 56d597737SShawn McCarney * you may not use this file except in compliance with the License. 66d597737SShawn McCarney * You may obtain a copy of the License at 76d597737SShawn McCarney * 86d597737SShawn McCarney * http://www.apache.org/licenses/LICENSE-2.0 96d597737SShawn McCarney * 106d597737SShawn McCarney * Unless required by applicable law or agreed to in writing, software 116d597737SShawn McCarney * distributed under the License is distributed on an "AS IS" BASIS, 126d597737SShawn McCarney * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 136d597737SShawn McCarney * See the License for the specific language governing permissions and 146d597737SShawn McCarney * limitations under the License. 156d597737SShawn McCarney */ 166d597737SShawn McCarney #pragma once 176d597737SShawn McCarney 186d597737SShawn McCarney #include "action.hpp" 196d597737SShawn McCarney #include "action_environment.hpp" 206d597737SShawn McCarney 216d597737SShawn McCarney #include <memory> 228a3db36aSShawn McCarney #include <string> 236d597737SShawn McCarney #include <utility> 246d597737SShawn McCarney #include <vector> 256d597737SShawn McCarney 266d597737SShawn McCarney namespace phosphor::power::regulators 276d597737SShawn McCarney { 286d597737SShawn McCarney 296d597737SShawn McCarney /** 306d597737SShawn McCarney * @class IfAction 316d597737SShawn McCarney * 326d597737SShawn McCarney * Performs actions based on whether a condition is true. 336d597737SShawn McCarney * 346d597737SShawn McCarney * Implements the "if" action in the JSON config file. The "if" action provides 356d597737SShawn McCarney * a standard if/then/else structure within the JSON config file. 366d597737SShawn McCarney * 376d597737SShawn McCarney * The "if" action contains three parts: 386d597737SShawn McCarney * - condition 396d597737SShawn McCarney * - then clause 406d597737SShawn McCarney * - else clause (optional) 416d597737SShawn McCarney * 426d597737SShawn McCarney * The condition is a single action. The action is executed to determine if the 436d597737SShawn McCarney * condition is true. 446d597737SShawn McCarney * 456d597737SShawn McCarney * If the condition is true, the actions in the "then" clause are executed. 466d597737SShawn McCarney * 476d597737SShawn McCarney * If the condition is false, the actions in the "else" clause are executed (if 486d597737SShawn McCarney * specified). 496d597737SShawn McCarney */ 506d597737SShawn McCarney class IfAction : public Action 516d597737SShawn McCarney { 526d597737SShawn McCarney public: 536d597737SShawn McCarney // Specify which compiler-generated methods we want 546d597737SShawn McCarney IfAction() = delete; 556d597737SShawn McCarney IfAction(const IfAction&) = delete; 566d597737SShawn McCarney IfAction(IfAction&&) = delete; 576d597737SShawn McCarney IfAction& operator=(const IfAction&) = delete; 586d597737SShawn McCarney IfAction& operator=(IfAction&&) = delete; 596d597737SShawn McCarney virtual ~IfAction() = default; 606d597737SShawn McCarney 616d597737SShawn McCarney /** 626d597737SShawn McCarney * Constructor. 636d597737SShawn McCarney * 646d597737SShawn McCarney * @param conditionAction action that tests whether condition is true 656d597737SShawn McCarney * @param thenActions actions to perform if condition is true 666d597737SShawn McCarney * @param elseActions actions to perform if condition is false (optional) 676d597737SShawn McCarney */ 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>>{})686d597737SShawn McCarney explicit IfAction(std::unique_ptr<Action> conditionAction, 696d597737SShawn McCarney std::vector<std::unique_ptr<Action>> thenActions, 706d597737SShawn McCarney std::vector<std::unique_ptr<Action>> elseActions = 716d597737SShawn McCarney std::vector<std::unique_ptr<Action>>{}) : 726d597737SShawn McCarney conditionAction{std::move(conditionAction)}, 736d597737SShawn McCarney thenActions{std::move(thenActions)}, elseActions{std::move(elseActions)} 74*0c9a33d6SAdriana Kobylak {} 756d597737SShawn McCarney 766d597737SShawn McCarney /** 776d597737SShawn McCarney * Executes the condition action specified in the constructor. 786d597737SShawn McCarney * 796d597737SShawn McCarney * If the condition action returns true, the actions in the "then" clause 806d597737SShawn McCarney * will be executed. Returns the return value of the last action in the 816d597737SShawn McCarney * "then" clause. 826d597737SShawn McCarney * 836d597737SShawn McCarney * If the condition action returns false, the actions in the "else" clause 846d597737SShawn McCarney * will be executed. Returns the return value of the last action in the 856d597737SShawn McCarney * "else" clause. If no "else" clause was specified, returns false. 866d597737SShawn McCarney * 876d597737SShawn McCarney * Throws an exception if an error occurs and an action cannot be 886d597737SShawn McCarney * successfully executed. 896d597737SShawn McCarney * 906d597737SShawn McCarney * @param environment action execution environment 916d597737SShawn McCarney * @return return value from last action in "then" or "else" clause 926d597737SShawn McCarney */ 936d597737SShawn McCarney virtual bool execute(ActionEnvironment& environment) override; 946d597737SShawn McCarney 956d597737SShawn McCarney /** 966d597737SShawn McCarney * Returns the action that tests whether the condition is true. 976d597737SShawn McCarney * 986d597737SShawn McCarney * @return condition action 996d597737SShawn McCarney */ getConditionAction() const1006d597737SShawn McCarney const std::unique_ptr<Action>& getConditionAction() const 1016d597737SShawn McCarney { 1026d597737SShawn McCarney return conditionAction; 1036d597737SShawn McCarney } 1046d597737SShawn McCarney 1056d597737SShawn McCarney /** 1066d597737SShawn McCarney * Returns the actions in the "then" clause. 1076d597737SShawn McCarney * 1086d597737SShawn McCarney * These actions are executed if the condition is true. 1096d597737SShawn McCarney * 1106d597737SShawn McCarney * @return then clause actions 1116d597737SShawn McCarney */ getThenActions() const1126d597737SShawn McCarney const std::vector<std::unique_ptr<Action>>& getThenActions() const 1136d597737SShawn McCarney { 1146d597737SShawn McCarney return thenActions; 1156d597737SShawn McCarney } 1166d597737SShawn McCarney 1176d597737SShawn McCarney /** 1186d597737SShawn McCarney * Returns the actions in the "else" clause. 1196d597737SShawn McCarney * 1206d597737SShawn McCarney * These actions are executed if the condition is false. 1216d597737SShawn McCarney * 1226d597737SShawn McCarney * @return else clause actions 1236d597737SShawn McCarney */ getElseActions() const1246d597737SShawn McCarney const std::vector<std::unique_ptr<Action>>& getElseActions() const 1256d597737SShawn McCarney { 1266d597737SShawn McCarney return elseActions; 1276d597737SShawn McCarney } 1286d597737SShawn McCarney 1298a3db36aSShawn McCarney /** 1308a3db36aSShawn McCarney * Returns a string description of this action. 1318a3db36aSShawn McCarney * 1328a3db36aSShawn McCarney * @return description of action 1338a3db36aSShawn McCarney */ toString() const1348a3db36aSShawn McCarney virtual std::string toString() const override 1358a3db36aSShawn McCarney { 1368a3db36aSShawn McCarney std::string description{"if: { condition: { ... }, then: [ ... ]"}; 1378a3db36aSShawn McCarney if (elseActions.size() > 0) 1388a3db36aSShawn McCarney { 1398a3db36aSShawn McCarney description += ", else: [ ... ]"; 1408a3db36aSShawn McCarney } 1418a3db36aSShawn McCarney description += " }"; 1428a3db36aSShawn McCarney return description; 1438a3db36aSShawn McCarney } 1448a3db36aSShawn McCarney 1456d597737SShawn McCarney private: 1466d597737SShawn McCarney /** 1476d597737SShawn McCarney * Action that tests whether the condition is true. 1486d597737SShawn McCarney */ 1496d597737SShawn McCarney std::unique_ptr<Action> conditionAction{}; 1506d597737SShawn McCarney 1516d597737SShawn McCarney /** 1526d597737SShawn McCarney * Actions in the "then" clause. Executed if condition is true. 1536d597737SShawn McCarney */ 1546d597737SShawn McCarney std::vector<std::unique_ptr<Action>> thenActions{}; 1556d597737SShawn McCarney 1566d597737SShawn McCarney /** 1576d597737SShawn McCarney * Actions in the "else" clause. Executed if condition is false. Optional. 1586d597737SShawn McCarney */ 1596d597737SShawn McCarney std::vector<std::unique_ptr<Action>> elseActions{}; 1606d597737SShawn McCarney }; 1616d597737SShawn McCarney 1626d597737SShawn McCarney } // namespace phosphor::power::regulators 163