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 MissingOwnerTarget - Action to set a target when an owner is missing
31  *
32  * Sets the fans to a configured target when any service owner associated to the
33  * group is missing. Once all services are functional and providing all the
34  * group data again, active fan target changes are allowed.
35  */
36 class MissingOwnerTarget :
37     public ActionBase,
38     public ActionRegister<MissingOwnerTarget>
39 {
40   public:
41     /* Name of this action */
42     static constexpr auto name = "set_target_on_missing_owner";
43 
44     MissingOwnerTarget() = delete;
45     MissingOwnerTarget(const MissingOwnerTarget&) = delete;
46     MissingOwnerTarget(MissingOwnerTarget&&) = delete;
47     MissingOwnerTarget& operator=(const MissingOwnerTarget&) = delete;
48     MissingOwnerTarget& operator=(MissingOwnerTarget&&) = delete;
49     ~MissingOwnerTarget() = default;
50 
51     /**
52      * @brief Set target on an owner missing
53      *
54      * @param[in] jsonObj - JSON configuration of this action
55      * @param[in] groups - Groups of dbus objects the action uses
56      */
57     MissingOwnerTarget(const json& jsonObj, const std::vector<Group>& groups);
58 
59     /**
60      * @brief Run the action
61      *
62      * Updates the services of the group, then determines if any of the
63      * services hosting the members of the group are not owned on dbus
64      * resulting in the zone's target being set/held at the configured target.
65      *
66      * @param[in] zone - Zone to run the action on
67      */
68     void run(Zone& zone) override;
69 
70   private:
71     /* Target for this action */
72     uint64_t _target;
73 
74     /**
75      * @brief Parse and set the target
76      *
77      * @param[in] jsonObj - JSON object for the action
78      *
79      * Sets the target to use when running the action
80      */
81     void setTarget(const json& jsonObj);
82 };
83 
84 } // namespace phosphor::fan::control::json
85