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