blob: 7341a3533fb8e2abd5982b569379859cad07c39a [file] [log] [blame]
Matthew Barthdc776c82021-02-25 16:06:16 -06001/**
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 "net_target_increase.hpp"
17
18#include "../manager.hpp"
19#include "../zone.hpp"
20#include "action.hpp"
21#include "group.hpp"
22
Matthew Barthdc776c82021-02-25 16:06:16 -060023#include <nlohmann/json.hpp>
24#include <phosphor-logging/log.hpp>
25
26#include <algorithm>
Patrick Williamsfbf47032023-07-17 12:27:34 -050027#include <format>
Matthew Barthdc776c82021-02-25 16:06:16 -060028#include <variant>
29
30namespace phosphor::fan::control::json
31{
32
33using json = nlohmann::json;
34using namespace phosphor::logging;
35
Matthew Barth19c77492021-04-08 10:06:06 -050036NetTargetIncrease::NetTargetIncrease(const json& jsonObj,
37 const std::vector<Group>& groups) :
38 ActionBase(jsonObj, groups)
Matthew Barthdc776c82021-02-25 16:06:16 -060039{
40 setState(jsonObj);
41 setDelta(jsonObj);
42}
43
Matthew Barth6d2476c2021-04-08 10:48:57 -050044void NetTargetIncrease::run(Zone& zone)
Matthew Barthdc776c82021-02-25 16:06:16 -060045{
Matt Spinlerbabe3122021-08-06 09:01:38 -050046 if (!_stateParameter.empty())
47 {
48 auto s = Manager::getParameter(_stateParameter);
49 if (!s)
50 {
Matt Spinlerbabe3122021-08-06 09:01:38 -050051 return;
52 }
53 _state = *s;
54 }
55
Matthew Barthdc776c82021-02-25 16:06:16 -060056 auto netDelta = zone.getIncDelta();
Matthew Barth6d2476c2021-04-08 10:48:57 -050057 for (const auto& group : _groups)
58 {
59 const auto& members = group.getMembers();
Patrick Williams61b73292023-05-10 07:50:12 -050060 std::for_each(members.begin(), members.end(),
61 [this, &zone, &group, &netDelta](const auto& member) {
62 try
63 {
64 auto value = Manager::getObjValueVariant(
65 member, group.getInterface(), group.getProperty());
66 if (std::holds_alternative<int64_t>(value) ||
67 std::holds_alternative<double>(value))
Matthew Barthdc776c82021-02-25 16:06:16 -060068 {
Patrick Williams61b73292023-05-10 07:50:12 -050069 // Where a group of int/doubles are greater than or
70 // equal to the state(some value) provided, request an
71 // increase of the configured delta times the difference
72 // between the group member's value and configured state
73 // value.
74 if (value >= _state)
Matthew Barthdc776c82021-02-25 16:06:16 -060075 {
Patrick Williams61b73292023-05-10 07:50:12 -050076 uint64_t incDelta = 0;
77 if (auto dblPtr = std::get_if<double>(&value))
Matthew Barthdc776c82021-02-25 16:06:16 -060078 {
Patrick Williams61b73292023-05-10 07:50:12 -050079 incDelta = static_cast<uint64_t>(
80 (*dblPtr - std::get<double>(_state)) * _delta);
Matthew Barthdc776c82021-02-25 16:06:16 -060081 }
Patrick Williams61b73292023-05-10 07:50:12 -050082 else
Matthew Barthdc776c82021-02-25 16:06:16 -060083 {
Patrick Williams61b73292023-05-10 07:50:12 -050084 // Increase by at least a single delta
85 // to attempt bringing under provided 'state'
86 auto deltaFactor =
87 std::max((std::get<int64_t>(value) -
88 std::get<int64_t>(_state)),
89 int64_t(1));
90 incDelta =
91 static_cast<uint64_t>(deltaFactor * _delta);
Matthew Barthdc776c82021-02-25 16:06:16 -060092 }
Patrick Williams61b73292023-05-10 07:50:12 -050093 netDelta = std::max(netDelta, incDelta);
Matthew Barthdc776c82021-02-25 16:06:16 -060094 }
95 }
Patrick Williams61b73292023-05-10 07:50:12 -050096 else if (std::holds_alternative<bool>(value))
Matthew Barthdc776c82021-02-25 16:06:16 -060097 {
Patrick Williams61b73292023-05-10 07:50:12 -050098 // Where a group of booleans equal the state(`true` or
99 // `false`) provided, request an increase of the
100 // configured delta
101 if (_state == value)
102 {
103 netDelta = std::max(netDelta, _delta);
104 }
Matthew Barthdc776c82021-02-25 16:06:16 -0600105 }
Patrick Williams61b73292023-05-10 07:50:12 -0500106 else if (std::holds_alternative<std::string>(value))
107 {
108 // Where a group of strings equal the state(some string)
109 // provided, request an increase of the configured delta
110 if (_state == value)
111 {
112 netDelta = std::max(netDelta, _delta);
113 }
114 }
115 else
116 {
117 // Unsupported group member type for this action
118 log<level::ERR>(
Patrick Williamsfbf47032023-07-17 12:27:34 -0500119 std::format("Action {}: Unsupported group member type "
Patrick Williams61b73292023-05-10 07:50:12 -0500120 "given. [object = {} : {} : {}]",
121 ActionBase::getName(), member,
122 group.getInterface(), group.getProperty())
123 .c_str());
124 }
125 }
126 catch (const std::out_of_range& oore)
127 {
128 // Property value not found, netDelta unchanged
129 }
130 });
Matthew Barth6d2476c2021-04-08 10:48:57 -0500131 }
Matthew Barthdc776c82021-02-25 16:06:16 -0600132 // Request increase to target
133 zone.requestIncrease(netDelta);
134}
135
136void NetTargetIncrease::setState(const json& jsonObj)
137{
Matt Spinlerbabe3122021-08-06 09:01:38 -0500138 if (jsonObj.contains("state"))
Matthew Barthdc776c82021-02-25 16:06:16 -0600139 {
Matt Spinlerbabe3122021-08-06 09:01:38 -0500140 _state = getJsonValue(jsonObj["state"]);
Matthew Barthdc776c82021-02-25 16:06:16 -0600141 }
Matt Spinlerbabe3122021-08-06 09:01:38 -0500142 else if (jsonObj.contains("state_parameter_name"))
143 {
144 _stateParameter = jsonObj["state_parameter_name"].get<std::string>();
145 }
146 else
147 {
148 throw ActionParseError{
149 ActionBase::getName(),
150 "Missing required state or state_parameter_name value"};
151 }
Matthew Barthdc776c82021-02-25 16:06:16 -0600152}
153
154void NetTargetIncrease::setDelta(const json& jsonObj)
155{
156 if (!jsonObj.contains("delta"))
157 {
158 throw ActionParseError{ActionBase::getName(),
159 "Missing required delta value"};
160 }
161 _delta = jsonObj["delta"].get<uint64_t>();
162}
163
164} // namespace phosphor::fan::control::json