xref: /openbmc/phosphor-fan-presence/control/json/actions/count_state_target.cpp (revision fbf4703f3de7fbdbd8388e946bd71c3b760b174c)
189c2fa14SMatthew Barth /**
289c2fa14SMatthew Barth  * Copyright © 2021 IBM Corporation
389c2fa14SMatthew Barth  *
489c2fa14SMatthew Barth  * Licensed under the Apache License, Version 2.0 (the "License");
589c2fa14SMatthew Barth  * you may not use this file except in compliance with the License.
689c2fa14SMatthew Barth  * You may obtain a copy of the License at
789c2fa14SMatthew Barth  *
889c2fa14SMatthew Barth  *     http://www.apache.org/licenses/LICENSE-2.0
989c2fa14SMatthew Barth  *
1089c2fa14SMatthew Barth  * Unless required by applicable law or agreed to in writing, software
1189c2fa14SMatthew Barth  * distributed under the License is distributed on an "AS IS" BASIS,
1289c2fa14SMatthew Barth  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1389c2fa14SMatthew Barth  * See the License for the specific language governing permissions and
1489c2fa14SMatthew Barth  * limitations under the License.
1589c2fa14SMatthew Barth  */
1689c2fa14SMatthew Barth #include "count_state_target.hpp"
1789c2fa14SMatthew Barth 
1889c2fa14SMatthew Barth #include "../manager.hpp"
1989c2fa14SMatthew Barth #include "../zone.hpp"
2089c2fa14SMatthew Barth #include "action.hpp"
2189c2fa14SMatthew Barth #include "group.hpp"
2289c2fa14SMatthew Barth 
2389c2fa14SMatthew Barth #include <nlohmann/json.hpp>
2489c2fa14SMatthew Barth 
25*fbf4703fSPatrick Williams #include <format>
26*fbf4703fSPatrick Williams 
2789c2fa14SMatthew Barth namespace phosphor::fan::control::json
2889c2fa14SMatthew Barth {
2989c2fa14SMatthew Barth 
3089c2fa14SMatthew Barth using json = nlohmann::json;
3189c2fa14SMatthew Barth 
CountStateTarget(const json & jsonObj,const std::vector<Group> & groups)3219c77494SMatthew Barth CountStateTarget::CountStateTarget(const json& jsonObj,
3319c77494SMatthew Barth                                    const std::vector<Group>& groups) :
3419c77494SMatthew Barth     ActionBase(jsonObj, groups)
3589c2fa14SMatthew Barth {
3689c2fa14SMatthew Barth     setCount(jsonObj);
3789c2fa14SMatthew Barth     setState(jsonObj);
3889c2fa14SMatthew Barth     setTarget(jsonObj);
3989c2fa14SMatthew Barth }
4089c2fa14SMatthew Barth 
run(Zone & zone)416d2476c9SMatthew Barth void CountStateTarget::run(Zone& zone)
4289c2fa14SMatthew Barth {
4389c2fa14SMatthew Barth     size_t numAtState = 0;
446d2476c9SMatthew Barth     for (const auto& group : _groups)
456d2476c9SMatthew Barth     {
4682a7d0b8SMatthew Barth         for (const auto& member : group.getMembers())
4789c2fa14SMatthew Barth         {
4889c2fa14SMatthew Barth             try
4989c2fa14SMatthew Barth             {
5089c2fa14SMatthew Barth                 if (Manager::getObjValueVariant(member, group.getInterface(),
5189c2fa14SMatthew Barth                                                 group.getProperty()) == _state)
5289c2fa14SMatthew Barth                 {
5389c2fa14SMatthew Barth                     numAtState++;
5489c2fa14SMatthew Barth                 }
5589c2fa14SMatthew Barth             }
5689c2fa14SMatthew Barth             catch (const std::out_of_range& oore)
5789c2fa14SMatthew Barth             {
5889c2fa14SMatthew Barth                 // Default to property not equal when not found
5989c2fa14SMatthew Barth             }
6089c2fa14SMatthew Barth             if (numAtState >= _count)
6189c2fa14SMatthew Barth             {
6289c2fa14SMatthew Barth                 break;
6389c2fa14SMatthew Barth             }
6489c2fa14SMatthew Barth         }
6582a7d0b8SMatthew Barth         if (numAtState >= _count)
6682a7d0b8SMatthew Barth         {
6782a7d0b8SMatthew Barth             break;
6889c2fa14SMatthew Barth         }
696d2476c9SMatthew Barth     }
7089c2fa14SMatthew Barth 
715368011eSMatthew Barth     // Update zone's target hold based on action results
72274f281bSMatt Spinler     zone.setTargetHold(getUniqueName(), _target, (numAtState >= _count));
7382a7d0b8SMatthew Barth }
7482a7d0b8SMatthew Barth 
setCount(const json & jsonObj)7589c2fa14SMatthew Barth void CountStateTarget::setCount(const json& jsonObj)
7689c2fa14SMatthew Barth {
7789c2fa14SMatthew Barth     if (!jsonObj.contains("count"))
7889c2fa14SMatthew Barth     {
7989c2fa14SMatthew Barth         throw ActionParseError{ActionBase::getName(),
8089c2fa14SMatthew Barth                                "Missing required count value"};
8189c2fa14SMatthew Barth     }
8289c2fa14SMatthew Barth     _count = jsonObj["count"].get<size_t>();
8389c2fa14SMatthew Barth }
8489c2fa14SMatthew Barth 
setState(const json & jsonObj)8589c2fa14SMatthew Barth void CountStateTarget::setState(const json& jsonObj)
8689c2fa14SMatthew Barth {
8789c2fa14SMatthew Barth     if (!jsonObj.contains("state"))
8889c2fa14SMatthew Barth     {
8989c2fa14SMatthew Barth         throw ActionParseError{ActionBase::getName(),
9089c2fa14SMatthew Barth                                "Missing required state value"};
9189c2fa14SMatthew Barth     }
9289c2fa14SMatthew Barth     _state = getJsonValue(jsonObj["state"]);
9389c2fa14SMatthew Barth }
9489c2fa14SMatthew Barth 
setTarget(const json & jsonObj)9589c2fa14SMatthew Barth void CountStateTarget::setTarget(const json& jsonObj)
9689c2fa14SMatthew Barth {
97bab94f29SMatthew Barth     if (!jsonObj.contains("target"))
9889c2fa14SMatthew Barth     {
9989c2fa14SMatthew Barth         throw ActionParseError{ActionBase::getName(),
100bab94f29SMatthew Barth                                "Missing required target value"};
10189c2fa14SMatthew Barth     }
102bab94f29SMatthew Barth     _target = jsonObj["target"].get<uint64_t>();
10389c2fa14SMatthew Barth }
10489c2fa14SMatthew Barth 
10589c2fa14SMatthew Barth } // namespace phosphor::fan::control::json
106