xref: /openbmc/phosphor-fan-presence/control/json/actions/missing_owner_target.cpp (revision 9d533806250cea56406bdd39e025f0d820c4ed90)
1070ee3c6SMatthew Barth /**
2070ee3c6SMatthew Barth  * Copyright © 2021 IBM Corporation
3070ee3c6SMatthew Barth  *
4070ee3c6SMatthew Barth  * Licensed under the Apache License, Version 2.0 (the "License");
5070ee3c6SMatthew Barth  * you may not use this file except in compliance with the License.
6070ee3c6SMatthew Barth  * You may obtain a copy of the License at
7070ee3c6SMatthew Barth  *
8070ee3c6SMatthew Barth  *     http://www.apache.org/licenses/LICENSE-2.0
9070ee3c6SMatthew Barth  *
10070ee3c6SMatthew Barth  * Unless required by applicable law or agreed to in writing, software
11070ee3c6SMatthew Barth  * distributed under the License is distributed on an "AS IS" BASIS,
12070ee3c6SMatthew Barth  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13070ee3c6SMatthew Barth  * See the License for the specific language governing permissions and
14070ee3c6SMatthew Barth  * limitations under the License.
15070ee3c6SMatthew Barth  */
16070ee3c6SMatthew Barth #include "missing_owner_target.hpp"
17070ee3c6SMatthew Barth 
18070ee3c6SMatthew Barth #include "../manager.hpp"
19070ee3c6SMatthew Barth #include "../zone.hpp"
20070ee3c6SMatthew Barth #include "group.hpp"
21070ee3c6SMatthew Barth 
22070ee3c6SMatthew Barth #include <nlohmann/json.hpp>
23070ee3c6SMatthew Barth 
24070ee3c6SMatthew Barth #include <algorithm>
25*fbf4703fSPatrick Williams #include <format>
26070ee3c6SMatthew Barth 
27070ee3c6SMatthew Barth namespace phosphor::fan::control::json
28070ee3c6SMatthew Barth {
29070ee3c6SMatthew Barth 
30070ee3c6SMatthew Barth using json = nlohmann::json;
31070ee3c6SMatthew Barth 
MissingOwnerTarget(const json & jsonObj,const std::vector<Group> & groups)3219c77494SMatthew Barth MissingOwnerTarget::MissingOwnerTarget(const json& jsonObj,
3319c77494SMatthew Barth                                        const std::vector<Group>& groups) :
3419c77494SMatthew Barth     ActionBase(jsonObj, groups)
35070ee3c6SMatthew Barth {
36070ee3c6SMatthew Barth     setTarget(jsonObj);
37070ee3c6SMatthew Barth }
38070ee3c6SMatthew Barth 
run(Zone & zone)396d2476c9SMatthew Barth void MissingOwnerTarget::run(Zone& zone)
406d2476c9SMatthew Barth {
416d2476c9SMatthew Barth     for (const auto& group : _groups)
42070ee3c6SMatthew Barth     {
43070ee3c6SMatthew Barth         const auto& members = group.getMembers();
44070ee3c6SMatthew Barth         auto isMissingOwner =
45070ee3c6SMatthew Barth             std::any_of(members.begin(), members.end(),
46070ee3c6SMatthew Barth                         [&intf = group.getInterface()](const auto& member) {
47070ee3c6SMatthew Barth                             return !Manager::hasOwner(member, intf);
48070ee3c6SMatthew Barth                         });
495368011eSMatthew Barth         // Update zone's target hold based on action results
505368011eSMatthew Barth         zone.setTargetHold(group.getName(), _target, isMissingOwner);
51070ee3c6SMatthew Barth     }
526d2476c9SMatthew Barth }
53070ee3c6SMatthew Barth 
setTarget(const json & jsonObj)54070ee3c6SMatthew Barth void MissingOwnerTarget::setTarget(const json& jsonObj)
55070ee3c6SMatthew Barth {
5655ed901bSMatthew Barth     if (!jsonObj.contains("target"))
57070ee3c6SMatthew Barth     {
58bfd7e1b7SMatthew Barth         throw ActionParseError{ActionBase::getName(),
5955ed901bSMatthew Barth                                "Missing required target value"};
60070ee3c6SMatthew Barth     }
6155ed901bSMatthew Barth     _target = jsonObj["target"].get<uint64_t>();
62070ee3c6SMatthew Barth }
63070ee3c6SMatthew Barth 
64070ee3c6SMatthew Barth } // namespace phosphor::fan::control::json
65