1*41e76f84SMatt Spinler /**
2*41e76f84SMatt Spinler  * Copyright © 2021 IBM Corporation
3*41e76f84SMatt Spinler  *
4*41e76f84SMatt Spinler  * Licensed under the Apache License, Version 2.0 (the "License");
5*41e76f84SMatt Spinler  * you may not use this file except in compliance with the License.
6*41e76f84SMatt Spinler  * You may obtain a copy of the License at
7*41e76f84SMatt Spinler  *
8*41e76f84SMatt Spinler  *     http://www.apache.org/licenses/LICENSE-2.0
9*41e76f84SMatt Spinler  *
10*41e76f84SMatt Spinler  * Unless required by applicable law or agreed to in writing, software
11*41e76f84SMatt Spinler  * distributed under the License is distributed on an "AS IS" BASIS,
12*41e76f84SMatt Spinler  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*41e76f84SMatt Spinler  * See the License for the specific language governing permissions and
14*41e76f84SMatt Spinler  * limitations under the License.
15*41e76f84SMatt Spinler  */
16*41e76f84SMatt Spinler #pragma once
17*41e76f84SMatt Spinler 
18*41e76f84SMatt Spinler #include "../zone.hpp"
19*41e76f84SMatt Spinler #include "action.hpp"
20*41e76f84SMatt Spinler #include "group.hpp"
21*41e76f84SMatt Spinler 
22*41e76f84SMatt Spinler #include <nlohmann/json.hpp>
23*41e76f84SMatt Spinler 
24*41e76f84SMatt Spinler namespace phosphor::fan::control::json
25*41e76f84SMatt Spinler {
26*41e76f84SMatt Spinler 
27*41e76f84SMatt Spinler using json = nlohmann::json;
28*41e76f84SMatt Spinler 
29*41e76f84SMatt Spinler /**
30*41e76f84SMatt Spinler  * @class GetManagedObjects
31*41e76f84SMatt Spinler  *
32*41e76f84SMatt Spinler  * This action adds the members of its groups to the object cache
33*41e76f84SMatt Spinler  * by using Manager::addObjects() which calls the GetManagedObjects
34*41e76f84SMatt Spinler  * D-Bus method to find and add the results.  When that is done,
35*41e76f84SMatt Spinler  * it then runs any actions listed in the JSON.
36*41e76f84SMatt Spinler  *
37*41e76f84SMatt Spinler  * This allows an action to run with the latest values in the cache
38*41e76f84SMatt Spinler  * without having to subscribe to propertiesChanged for them all.
39*41e76f84SMatt Spinler  *
40*41e76f84SMatt Spinler  * An example config is:
41*41e76f84SMatt Spinler  *
42*41e76f84SMatt Spinler  *    "actions": [
43*41e76f84SMatt Spinler  *      {
44*41e76f84SMatt Spinler  *        "name": "get_managed_objects",
45*41e76f84SMatt Spinler  *        "groups": [
46*41e76f84SMatt Spinler  *          {
47*41e76f84SMatt Spinler  *            "name": "the_temps",
48*41e76f84SMatt Spinler  *            "interface": "xyz.openbmc_project.Sensor.Value",
49*41e76f84SMatt Spinler  *            "property": {
50*41e76f84SMatt Spinler  *              "name": "Value"
51*41e76f84SMatt Spinler  *            }
52*41e76f84SMatt Spinler  *          }
53*41e76f84SMatt Spinler  *        ],
54*41e76f84SMatt Spinler  *        "actions": [
55*41e76f84SMatt Spinler  *          {
56*41e76f84SMatt Spinler  *            "name": "set_net_increase_target",
57*41e76f84SMatt Spinler  *            "state": 30,
58*41e76f84SMatt Spinler  *            "delta": 100
59*41e76f84SMatt Spinler  *          }
60*41e76f84SMatt Spinler  *        ]
61*41e76f84SMatt Spinler  *     }
62*41e76f84SMatt Spinler  *   ]
63*41e76f84SMatt Spinler  **/
64*41e76f84SMatt Spinler class GetManagedObjects :
65*41e76f84SMatt Spinler     public ActionBase,
66*41e76f84SMatt Spinler     public ActionRegister<GetManagedObjects>
67*41e76f84SMatt Spinler {
68*41e76f84SMatt Spinler   public:
69*41e76f84SMatt Spinler     /* Name of this action */
70*41e76f84SMatt Spinler     static constexpr auto name = "get_managed_objects";
71*41e76f84SMatt Spinler 
72*41e76f84SMatt Spinler     GetManagedObjects() = delete;
73*41e76f84SMatt Spinler     GetManagedObjects(const GetManagedObjects&) = delete;
74*41e76f84SMatt Spinler     GetManagedObjects(GetManagedObjects&&) = delete;
75*41e76f84SMatt Spinler     GetManagedObjects& operator=(const GetManagedObjects&) = delete;
76*41e76f84SMatt Spinler     GetManagedObjects& operator=(GetManagedObjects&&) = delete;
77*41e76f84SMatt Spinler     ~GetManagedObjects() = default;
78*41e76f84SMatt Spinler 
79*41e76f84SMatt Spinler     /**
80*41e76f84SMatt Spinler      * @brief Parse the JSON to set the members
81*41e76f84SMatt Spinler      *
82*41e76f84SMatt Spinler      * @param[in] jsonObj - JSON configuration of this action
83*41e76f84SMatt Spinler      * @param[in] groups - Groups of dbus objects the action uses
84*41e76f84SMatt Spinler      */
85*41e76f84SMatt Spinler     GetManagedObjects(const json& jsonObj, const std::vector<Group>& groups);
86*41e76f84SMatt Spinler 
87*41e76f84SMatt Spinler     /**
88*41e76f84SMatt Spinler      * @brief Run the action.
89*41e76f84SMatt Spinler      *
90*41e76f84SMatt Spinler      * @param[in] zone - Zone to run the action on
91*41e76f84SMatt Spinler      */
92*41e76f84SMatt Spinler     void run(Zone& zone) override;
93*41e76f84SMatt Spinler 
94*41e76f84SMatt Spinler     /**
95*41e76f84SMatt Spinler      * @brief Set the zones on the action and the embedded actions
96*41e76f84SMatt Spinler      *
97*41e76f84SMatt Spinler      * @param[in] zones - Zones for the action and timer's actions
98*41e76f84SMatt Spinler      *
99*41e76f84SMatt Spinler      * Sets the zones on this action and the timer's actions to run against
100*41e76f84SMatt Spinler      */
101*41e76f84SMatt Spinler     void setZones(std::vector<std::reference_wrapper<Zone>>& zones) override;
102*41e76f84SMatt Spinler 
103*41e76f84SMatt Spinler   private:
104*41e76f84SMatt Spinler     /**
105*41e76f84SMatt Spinler      * @brief Parse and set the list of actions
106*41e76f84SMatt Spinler      *
107*41e76f84SMatt Spinler      * @param[in] jsonObj - JSON object for the action
108*41e76f84SMatt Spinler      *
109*41e76f84SMatt Spinler      * Sets the list of actions that is run when this action runs
110*41e76f84SMatt Spinler      */
111*41e76f84SMatt Spinler     void setActions(const json& jsonObj);
112*41e76f84SMatt Spinler 
113*41e76f84SMatt Spinler     /* List of actions to be called when this action runs */
114*41e76f84SMatt Spinler     std::vector<std::unique_ptr<ActionBase>> _actions;
115*41e76f84SMatt Spinler };
116*41e76f84SMatt Spinler 
117*41e76f84SMatt Spinler } // namespace phosphor::fan::control::json
118