blob: a9fe8d14bb10b524d134409308deb3e049a0a735 [file] [log] [blame]
Matthew Barth45c44ea2021-03-03 13:16:14 -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_decrease.hpp"
17
18#include "../manager.hpp"
19#include "../zone.hpp"
20#include "action.hpp"
21#include "group.hpp"
22
Matthew Barth45c44ea2021-03-03 13:16:14 -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 Barth45c44ea2021-03-03 13:16:14 -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 -050036NetTargetDecrease::NetTargetDecrease(const json& jsonObj,
37 const std::vector<Group>& groups) :
38 ActionBase(jsonObj, groups)
Matthew Barth45c44ea2021-03-03 13:16:14 -060039{
40 setState(jsonObj);
41 setDelta(jsonObj);
42}
43
Matthew Barth6d2476c2021-04-08 10:48:57 -050044void NetTargetDecrease::run(Zone& zone)
Matthew Barth45c44ea2021-03-03 13:16:14 -060045{
Matthew Barth20afdda2021-12-01 14:30:50 -060046 if (!_stateParameter.empty())
47 {
48 auto s = Manager::getParameter(_stateParameter);
49 if (!s)
50 {
Matthew Barth20afdda2021-12-01 14:30:50 -060051 return;
52 }
53 _state = *s;
54 }
55
Matthew Barth45c44ea2021-03-03 13:16:14 -060056 auto netDelta = zone.getDecDelta();
Matthew Barth6d2476c2021-04-08 10:48:57 -050057 for (const auto& group : _groups)
Matthew Barth45c44ea2021-03-03 13:16:14 -060058 {
Matthew Barth6d2476c2021-04-08 10:48:57 -050059 for (const auto& member : group.getMembers())
Matthew Barth45c44ea2021-03-03 13:16:14 -060060 {
Matthew Barth6d2476c2021-04-08 10:48:57 -050061 try
Matthew Barth45c44ea2021-03-03 13:16:14 -060062 {
Matthew Barth6d2476c2021-04-08 10:48:57 -050063 auto value = Manager::getObjValueVariant(
64 member, group.getInterface(), group.getProperty());
65 if (std::holds_alternative<int64_t>(value) ||
66 std::holds_alternative<double>(value))
Matthew Barth45c44ea2021-03-03 13:16:14 -060067 {
Matthew Barth6d2476c2021-04-08 10:48:57 -050068 if (value >= _state)
69 {
70 // No decrease allowed for this group
71 netDelta = 0;
72 break;
73 }
74 else
75 {
76 // Decrease factor is the difference in configured state
77 // to the current value's state
78 uint64_t deltaFactor = 0;
79 if (auto dblPtr = std::get_if<double>(&value))
80 {
81 deltaFactor = static_cast<uint64_t>(
82 std::get<double>(_state) - *dblPtr);
83 }
84 else
85 {
86 deltaFactor = static_cast<uint64_t>(
87 std::get<int64_t>(_state) -
88 std::get<int64_t>(value));
89 }
90
91 // Multiply the decrease factor by the configured delta
92 // to get the net decrease delta for the given group
93 // member. The lowest net decrease delta of the entire
94 // group is the decrease requested.
95 if (netDelta == 0)
96 {
97 netDelta = deltaFactor * _delta;
98 }
99 else
100 {
101 netDelta = std::min(netDelta, deltaFactor * _delta);
102 }
103 }
104 }
105 else if (std::holds_alternative<bool>(value) ||
106 std::holds_alternative<std::string>(value))
107 {
108 // Where a group of booleans or strings equal the state
109 // provided, request a decrease of the configured delta
110 if (_state == value)
111 {
112 if (netDelta == 0)
113 {
114 netDelta = _delta;
115 }
116 else
117 {
118 netDelta = std::min(netDelta, _delta);
119 }
120 }
Matthew Barth45c44ea2021-03-03 13:16:14 -0600121 }
122 else
123 {
Matthew Barth6d2476c2021-04-08 10:48:57 -0500124 // Unsupported group member type for this action
125 log<level::ERR>(
Patrick Williamsfbf47032023-07-17 12:27:34 -0500126 std::format("Action {}: Unsupported group member type "
Matthew Barth6d2476c2021-04-08 10:48:57 -0500127 "given. [object = {} : {} : {}]",
128 ActionBase::getName(), member,
129 group.getInterface(), group.getProperty())
130 .c_str());
Matthew Barth45c44ea2021-03-03 13:16:14 -0600131 }
132 }
Matthew Barth6d2476c2021-04-08 10:48:57 -0500133 catch (const std::out_of_range& oore)
Matthew Barth45c44ea2021-03-03 13:16:14 -0600134 {
Matthew Barth6d2476c2021-04-08 10:48:57 -0500135 // Property value not found, netDelta unchanged
Matthew Barth45c44ea2021-03-03 13:16:14 -0600136 }
137 }
Matthew Barth6d2476c2021-04-08 10:48:57 -0500138 // Update group's decrease allowed state
139 zone.setDecreaseAllow(group.getName(), !(netDelta == 0));
Matthew Barth45c44ea2021-03-03 13:16:14 -0600140 }
Matthew Barth45c44ea2021-03-03 13:16:14 -0600141 // Request target decrease to occur on decrease interval
142 zone.requestDecrease(netDelta);
143}
144
145void NetTargetDecrease::setState(const json& jsonObj)
146{
Matthew Barth20afdda2021-12-01 14:30:50 -0600147 if (jsonObj.contains("state"))
Matthew Barth45c44ea2021-03-03 13:16:14 -0600148 {
Matthew Barth20afdda2021-12-01 14:30:50 -0600149 _state = getJsonValue(jsonObj["state"]);
Matthew Barth45c44ea2021-03-03 13:16:14 -0600150 }
Matthew Barth20afdda2021-12-01 14:30:50 -0600151 else if (jsonObj.contains("state_parameter_name"))
152 {
153 _stateParameter = jsonObj["state_parameter_name"].get<std::string>();
154 }
155 else
156 {
157 throw ActionParseError{
158 ActionBase::getName(),
159 "Missing required state or state_parameter_name value"};
160 }
Matthew Barth45c44ea2021-03-03 13:16:14 -0600161}
162
163void NetTargetDecrease::setDelta(const json& jsonObj)
164{
165 if (!jsonObj.contains("delta"))
166 {
167 throw ActionParseError{ActionBase::getName(),
168 "Missing required delta value"};
169 }
170 _delta = jsonObj["delta"].get<uint64_t>();
171}
172
173} // namespace phosphor::fan::control::json