xref: /openbmc/phosphor-fan-presence/control/json/actions/missing_owner_target.cpp (revision 9d533806250cea56406bdd39e025f0d820c4ed90)
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 #include "missing_owner_target.hpp"
17 
18 #include "../manager.hpp"
19 #include "../zone.hpp"
20 #include "group.hpp"
21 
22 #include <nlohmann/json.hpp>
23 
24 #include <algorithm>
25 #include <format>
26 
27 namespace phosphor::fan::control::json
28 {
29 
30 using json = nlohmann::json;
31 
MissingOwnerTarget(const json & jsonObj,const std::vector<Group> & groups)32 MissingOwnerTarget::MissingOwnerTarget(const json& jsonObj,
33                                        const std::vector<Group>& groups) :
34     ActionBase(jsonObj, groups)
35 {
36     setTarget(jsonObj);
37 }
38 
run(Zone & zone)39 void MissingOwnerTarget::run(Zone& zone)
40 {
41     for (const auto& group : _groups)
42     {
43         const auto& members = group.getMembers();
44         auto isMissingOwner =
45             std::any_of(members.begin(), members.end(),
46                         [&intf = group.getInterface()](const auto& member) {
47                             return !Manager::hasOwner(member, intf);
48                         });
49         // Update zone's target hold based on action results
50         zone.setTargetHold(group.getName(), _target, isMissingOwner);
51     }
52 }
53 
setTarget(const json & jsonObj)54 void MissingOwnerTarget::setTarget(const json& jsonObj)
55 {
56     if (!jsonObj.contains("target"))
57     {
58         throw ActionParseError{ActionBase::getName(),
59                                "Missing required target value"};
60     }
61     _target = jsonObj["target"].get<uint64_t>();
62 }
63 
64 } // namespace phosphor::fan::control::json
65