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