blob: cc08cfa13e9d69e2d79a59f42cceea67495e81bd [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
23#include <fmt/format.h>
24
25#include <nlohmann/json.hpp>
26#include <phosphor-logging/log.hpp>
27
28#include <algorithm>
29#include <variant>
30
31namespace phosphor::fan::control::json
32{
33
34using json = nlohmann::json;
35using namespace phosphor::logging;
36
Matthew Barth19c77492021-04-08 10:06:06 -050037NetTargetIncrease::NetTargetIncrease(const json& jsonObj,
38 const std::vector<Group>& groups) :
39 ActionBase(jsonObj, groups)
Matthew Barthdc776c82021-02-25 16:06:16 -060040{
41 setState(jsonObj);
42 setDelta(jsonObj);
43}
44
Matthew Barth6d2476c2021-04-08 10:48:57 -050045void NetTargetIncrease::run(Zone& zone)
Matthew Barthdc776c82021-02-25 16:06:16 -060046{
Matt Spinlerbabe3122021-08-06 09:01:38 -050047
48 if (!_stateParameter.empty())
49 {
50 auto s = Manager::getParameter(_stateParameter);
51 if (!s)
52 {
Matt Spinlerbabe3122021-08-06 09:01:38 -050053 return;
54 }
55 _state = *s;
56 }
57
Matthew Barthdc776c82021-02-25 16:06:16 -060058 auto netDelta = zone.getIncDelta();
Matthew Barth6d2476c2021-04-08 10:48:57 -050059 for (const auto& group : _groups)
60 {
61 const auto& members = group.getMembers();
62 std::for_each(
63 members.begin(), members.end(),
64 [this, &zone, &group, &netDelta](const auto& member) {
65 try
Matthew Barthdc776c82021-02-25 16:06:16 -060066 {
Matthew Barth6d2476c2021-04-08 10:48:57 -050067 auto value = Manager::getObjValueVariant(
68 member, group.getInterface(), group.getProperty());
69 if (std::holds_alternative<int64_t>(value) ||
70 std::holds_alternative<double>(value))
Matthew Barthdc776c82021-02-25 16:06:16 -060071 {
Matthew Barth6d2476c2021-04-08 10:48:57 -050072 // Where a group of int/doubles are greater than or
73 // equal to the state(some value) provided, request an
74 // increase of the configured delta times the difference
75 // between the group member's value and configured state
76 // value.
77 if (value >= _state)
Matthew Barthdc776c82021-02-25 16:06:16 -060078 {
Matthew Barth6d2476c2021-04-08 10:48:57 -050079 uint64_t incDelta = 0;
80 if (auto dblPtr = std::get_if<double>(&value))
81 {
82 incDelta = static_cast<uint64_t>(
83 (*dblPtr - std::get<double>(_state)) *
84 _delta);
85 }
86 else
87 {
88 // Increase by at least a single delta
89 // to attempt bringing under provided 'state'
90 auto deltaFactor =
91 std::max((std::get<int64_t>(value) -
92 std::get<int64_t>(_state)),
Mike Cappsb2e9a4f2022-06-13 10:15:42 -040093 int64_t(1));
Matthew Barth6d2476c2021-04-08 10:48:57 -050094 incDelta =
95 static_cast<uint64_t>(deltaFactor * _delta);
96 }
97 netDelta = std::max(netDelta, incDelta);
Matthew Barthdc776c82021-02-25 16:06:16 -060098 }
Matthew Barth6d2476c2021-04-08 10:48:57 -050099 }
100 else if (std::holds_alternative<bool>(value))
101 {
102 // Where a group of booleans equal the state(`true` or
103 // `false`) provided, request an increase of the
104 // configured delta
105 if (_state == value)
Matthew Barthdc776c82021-02-25 16:06:16 -0600106 {
Matthew Barth6d2476c2021-04-08 10:48:57 -0500107 netDelta = std::max(netDelta, _delta);
Matthew Barthdc776c82021-02-25 16:06:16 -0600108 }
Matthew Barthdc776c82021-02-25 16:06:16 -0600109 }
Matthew Barth6d2476c2021-04-08 10:48:57 -0500110 else if (std::holds_alternative<std::string>(value))
Matthew Barthdc776c82021-02-25 16:06:16 -0600111 {
Matthew Barth6d2476c2021-04-08 10:48:57 -0500112 // Where a group of strings equal the state(some string)
113 // provided, request an increase of the configured delta
114 if (_state == value)
115 {
116 netDelta = std::max(netDelta, _delta);
117 }
Matthew Barthdc776c82021-02-25 16:06:16 -0600118 }
Matthew Barth6d2476c2021-04-08 10:48:57 -0500119 else
Matthew Barthdc776c82021-02-25 16:06:16 -0600120 {
Matthew Barth6d2476c2021-04-08 10:48:57 -0500121 // Unsupported group member type for this action
122 log<level::ERR>(
123 fmt::format(
124 "Action {}: Unsupported group member type "
125 "given. [object = {} : {} : {}]",
126 ActionBase::getName(), member,
127 group.getInterface(), group.getProperty())
128 .c_str());
Matthew Barthdc776c82021-02-25 16:06:16 -0600129 }
130 }
Matthew Barth6d2476c2021-04-08 10:48:57 -0500131 catch (const std::out_of_range& oore)
Matthew Barthdc776c82021-02-25 16:06:16 -0600132 {
Matthew Barth6d2476c2021-04-08 10:48:57 -0500133 // Property value not found, netDelta unchanged
Matthew Barthdc776c82021-02-25 16:06:16 -0600134 }
Matthew Barth6d2476c2021-04-08 10:48:57 -0500135 });
136 }
Matthew Barthdc776c82021-02-25 16:06:16 -0600137 // Request increase to target
138 zone.requestIncrease(netDelta);
139}
140
141void NetTargetIncrease::setState(const json& jsonObj)
142{
Matt Spinlerbabe3122021-08-06 09:01:38 -0500143 if (jsonObj.contains("state"))
Matthew Barthdc776c82021-02-25 16:06:16 -0600144 {
Matt Spinlerbabe3122021-08-06 09:01:38 -0500145 _state = getJsonValue(jsonObj["state"]);
Matthew Barthdc776c82021-02-25 16:06:16 -0600146 }
Matt Spinlerbabe3122021-08-06 09:01:38 -0500147 else if (jsonObj.contains("state_parameter_name"))
148 {
149 _stateParameter = jsonObj["state_parameter_name"].get<std::string>();
150 }
151 else
152 {
153 throw ActionParseError{
154 ActionBase::getName(),
155 "Missing required state or state_parameter_name value"};
156 }
Matthew Barthdc776c82021-02-25 16:06:16 -0600157}
158
159void NetTargetIncrease::setDelta(const json& jsonObj)
160{
161 if (!jsonObj.contains("delta"))
162 {
163 throw ActionParseError{ActionBase::getName(),
164 "Missing required delta value"};
165 }
166 _delta = jsonObj["delta"].get<uint64_t>();
167}
168
169} // namespace phosphor::fan::control::json