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 namespace phosphor::fan::control::json
25 {
26 
27 using json = nlohmann::json;
28 
29 /**
30  * @class GetManagedObjects
31  *
32  * This action adds the members of its groups to the object cache
33  * by using Manager::addObjects() which calls the GetManagedObjects
34  * D-Bus method to find and add the results.  When that is done,
35  * it then runs any actions listed in the JSON.
36  *
37  * This allows an action to run with the latest values in the cache
38  * without having to subscribe to propertiesChanged for them all.
39  *
40  * An example config is:
41  *
42  *    "actions": [
43  *      {
44  *        "name": "get_managed_objects",
45  *        "groups": [
46  *          {
47  *            "name": "the_temps",
48  *            "interface": "xyz.openbmc_project.Sensor.Value",
49  *            "property": {
50  *              "name": "Value"
51  *            }
52  *          }
53  *        ],
54  *        "actions": [
55  *          {
56  *            "name": "set_net_increase_target",
57  *            "state": 30,
58  *            "delta": 100
59  *          }
60  *        ]
61  *     }
62  *   ]
63  **/
64 class GetManagedObjects :
65     public ActionBase,
66     public ActionRegister<GetManagedObjects>
67 {
68   public:
69     /* Name of this action */
70     static constexpr auto name = "get_managed_objects";
71 
72     GetManagedObjects() = delete;
73     GetManagedObjects(const GetManagedObjects&) = delete;
74     GetManagedObjects(GetManagedObjects&&) = delete;
75     GetManagedObjects& operator=(const GetManagedObjects&) = delete;
76     GetManagedObjects& operator=(GetManagedObjects&&) = delete;
77     ~GetManagedObjects() = default;
78 
79     /**
80      * @brief Parse the JSON to set the members
81      *
82      * @param[in] jsonObj - JSON configuration of this action
83      * @param[in] groups - Groups of dbus objects the action uses
84      */
85     GetManagedObjects(const json& jsonObj, const std::vector<Group>& groups);
86 
87     /**
88      * @brief Run the action.
89      *
90      * @param[in] zone - Zone to run the action on
91      */
92     void run(Zone& zone) override;
93 
94     /**
95      * @brief Set the zones on the action and the embedded actions
96      *
97      * @param[in] zones - Zones for the action and timer's actions
98      *
99      * Sets the zones on this action and the timer's actions to run against
100      */
101     void setZones(std::vector<std::reference_wrapper<Zone>>& zones) override;
102 
103   private:
104     /**
105      * @brief Parse and set the list of actions
106      *
107      * @param[in] jsonObj - JSON object for the action
108      *
109      * Sets the list of actions that is run when this action runs
110      */
111     void setActions(const json& jsonObj);
112 
113     /* List of actions to be called when this action runs */
114     std::vector<std::unique_ptr<ActionBase>> _actions;
115 };
116 
117 } // namespace phosphor::fan::control::json
118