1 /**
2  * Copyright © 2021 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 "../zone.hpp"
19 #include "action.hpp"
20 #include "group.hpp"
21 
22 #include <nlohmann/json.hpp>
23 
24 #include <optional>
25 
26 namespace phosphor::fan::control::json
27 {
28 
29 using json = nlohmann::json;
30 
31 /**
32  * @class NetTargetIncrease - Action to determine the net target increase to
33  * request
34  *
35  * Calculates the net target increase to be requested based on the value of each
36  * property given within a group. The net target increase is the maximum delta
37  * determined from all of the properties of the group. This net target increase
38  * is the increase change that's requested to the current target of a zone.
39  */
40 class NetTargetIncrease :
41     public ActionBase,
42     public ActionRegister<NetTargetIncrease>
43 {
44   public:
45     /* Name of this action */
46     static constexpr auto name = "set_net_increase_speed";
47 
48     NetTargetIncrease() = delete;
49     NetTargetIncrease(const NetTargetIncrease&) = delete;
50     NetTargetIncrease(NetTargetIncrease&&) = delete;
51     NetTargetIncrease& operator=(const NetTargetIncrease&) = delete;
52     NetTargetIncrease& operator=(NetTargetIncrease&&) = delete;
53     ~NetTargetIncrease() = default;
54 
55     /**
56      * @brief Determine/Set the net target increase
57      *
58      * @param[in] jsonObj - JSON configuration of this action
59      * @param[in] groups - Groups of dbus objects the action uses
60      */
61     NetTargetIncrease(const json& jsonObj, const std::vector<Group>& groups);
62 
63     /**
64      * @brief Run the action
65      *
66      * Determines the net target increase delta to be requested based on the
67      * property values of the group of dbus objects and requests this target
68      * increase on the zone. The property values of the group is compared to
69      * the configured state value to determine if an increase delta should be
70      * requested. For boolean & string values, the configured delta is
71      * requested when a property of the group equals the configured state. For
72      * integer & double values, the configured delta is multiplied by the
73      * difference in property value and the configured state resulting in a net
74      * target increase for each group member. After all members of the group of
75      * dbus objects are processed, the maximum net target increase calculated is
76      * requested on the zone.
77      *
78      * @param[in] zone - Zone to run the action on
79      */
80     void run(Zone& zone) override;
81 
82   private:
83     /* State the members must be at to increase the rarget */
84     PropertyVariantType _state;
85 
86     /* Increase delta for this action */
87     uint64_t _delta;
88 
89     /**
90      * @brief Parse and set the state
91      *
92      * @param[in] jsonObj - JSON object for the action
93      *
94      * Sets the state to compare members to
95      */
96     void setState(const json& jsonObj);
97 
98     /**
99      * @brief Parse and set the delta
100      *
101      * @param[in] jsonObj - JSON object for the action
102      *
103      * Sets the increase delta to use when running the action
104      */
105     void setDelta(const json& jsonObj);
106 };
107 
108 } // namespace phosphor::fan::control::json
109