blob: ccfd8974658d106766f9d36d20c1fa8c481d37c9 [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
37NetTargetIncrease::NetTargetIncrease(const json& jsonObj) : ActionBase(jsonObj)
38{
39 setState(jsonObj);
40 setDelta(jsonObj);
41}
42
43void NetTargetIncrease::run(Zone& zone, const Group& group)
44{
45 auto netDelta = zone.getIncDelta();
46 const auto& members = group.getMembers();
47 std::for_each(
48 members.begin(), members.end(),
49 [this, &zone, &group, &netDelta](const auto& member) {
50 try
51 {
52 auto value = Manager::getObjValueVariant(
53 member, group.getInterface(), group.getProperty());
54 if (std::holds_alternative<int64_t>(value) ||
55 std::holds_alternative<double>(value))
56 {
57 // Where a group of int/doubles are greater than or equal
58 // to the state(some value) provided, request an increase
59 // of the configured delta times the difference between
60 // the group member's value and configured state value.
61 if (value >= _state)
62 {
63 uint64_t incDelta = 0;
64 if (auto dblPtr = std::get_if<double>(&value))
65 {
66 incDelta = static_cast<uint64_t>(
67 (*dblPtr - std::get<double>(_state)) * _delta);
68 }
69 else
70 {
71 // Increase by at least a single delta
72 // to attempt bringing under provided 'state'
73 auto deltaFactor =
74 std::max((std::get<int64_t>(value) -
75 std::get<int64_t>(_state)),
76 1ll);
77 incDelta =
78 static_cast<uint64_t>(deltaFactor * _delta);
79 }
80 netDelta = std::max(netDelta, incDelta);
81 }
82 }
83 else if (std::holds_alternative<bool>(value))
84 {
85 // Where a group of booleans equal the state(`true` or
86 // `false`) provided, request an increase of the configured
87 // delta
88 if (_state == value)
89 {
90 netDelta = std::max(netDelta, _delta);
91 }
92 }
93 else if (std::holds_alternative<std::string>(value))
94 {
95 // Where a group of strings equal the state(some string)
96 // provided, request an increase of the configured delta
97 if (_state == value)
98 {
99 netDelta = std::max(netDelta, _delta);
100 }
101 }
102 else
103 {
104 // Unsupported group member type for this action
105 log<level::ERR>(
106 fmt::format("Action {}: Unsupported group member type "
107 "given. [object = {} : {} : {}]",
108 ActionBase::getName(), member,
109 group.getInterface(), group.getProperty())
110 .c_str());
111 }
112 }
113 catch (const std::out_of_range& oore)
114 {
115 // Property value not found, netDelta unchanged
116 }
117 });
118 // Request increase to target
119 zone.requestIncrease(netDelta);
120}
121
122void NetTargetIncrease::setState(const json& jsonObj)
123{
124 if (!jsonObj.contains("state"))
125 {
126 throw ActionParseError{ActionBase::getName(),
127 "Missing required state value"};
128 }
129 _state = getJsonValue(jsonObj["state"]);
130}
131
132void NetTargetIncrease::setDelta(const json& jsonObj)
133{
134 if (!jsonObj.contains("delta"))
135 {
136 throw ActionParseError{ActionBase::getName(),
137 "Missing required delta value"};
138 }
139 _delta = jsonObj["delta"].get<uint64_t>();
140}
141
142} // namespace phosphor::fan::control::json