blob: 4664238cc59d12b8308f7de30d4ca06d6071473b [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
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
37NetTargetDecrease::NetTargetDecrease(const json& jsonObj) : ActionBase(jsonObj)
38{
39 setState(jsonObj);
40 setDelta(jsonObj);
41}
42
43void NetTargetDecrease::run(Zone& zone, const Group& group)
44{
45 auto netDelta = zone.getDecDelta();
46 for (const auto& member : group.getMembers())
47 {
48 try
49 {
50 auto value = Manager::getObjValueVariant(
51 member, group.getInterface(), group.getProperty());
52 if (std::holds_alternative<int64_t>(value) ||
53 std::holds_alternative<double>(value))
54 {
55 if (value >= _state)
56 {
57 // No decrease allowed for this group
58 netDelta = 0;
59 break;
60 }
61 else
62 {
63 // Decrease factor is the difference in configured state to
64 // the current value's state
65 uint64_t deltaFactor = 0;
66 if (auto dblPtr = std::get_if<double>(&value))
67 {
68 deltaFactor = static_cast<uint64_t>(
69 std::get<double>(_state) - *dblPtr);
70 }
71 else
72 {
73 deltaFactor =
74 static_cast<uint64_t>(std::get<int64_t>(_state) -
75 std::get<int64_t>(value));
76 }
77
78 // Multiply the decrease factor by the configured delta to
79 // get the net decrease delta for the given group member.
80 // The lowest net decrease delta of the entire group is the
81 // decrease requested.
82 if (netDelta == 0)
83 {
84 netDelta = deltaFactor * _delta;
85 }
86 else
87 {
88 netDelta = std::min(netDelta, deltaFactor * _delta);
89 }
90 }
91 }
92 else if (std::holds_alternative<bool>(value) ||
93 std::holds_alternative<std::string>(value))
94 {
95 // Where a group of booleans or strings equal the state
96 // provided, request a decrease of the configured delta
97 if (_state == value)
98 {
99 if (netDelta == 0)
100 {
101 netDelta = _delta;
102 }
103 else
104 {
105 netDelta = std::min(netDelta, _delta);
106 }
107 }
108 }
109 else
110 {
111 // Unsupported group member type for this action
112 log<level::ERR>(
113 fmt::format("Action {}: Unsupported group member type "
114 "given. [object = {} : {} : {}]",
115 ActionBase::getName(), member,
116 group.getInterface(), group.getProperty())
117 .c_str());
118 }
119 }
120 catch (const std::out_of_range& oore)
121 {
122 // Property value not found, netDelta unchanged
123 }
124 }
125 // Update group's decrease allowed state
126 zone.setDecreaseAllow(group.getName(), !(netDelta == 0));
127 // Request target decrease to occur on decrease interval
128 zone.requestDecrease(netDelta);
129}
130
131void NetTargetDecrease::setState(const json& jsonObj)
132{
133 if (!jsonObj.contains("state"))
134 {
135 throw ActionParseError{ActionBase::getName(),
136 "Missing required state value"};
137 }
138 _state = getJsonValue(jsonObj["state"]);
139}
140
141void NetTargetDecrease::setDelta(const json& jsonObj)
142{
143 if (!jsonObj.contains("delta"))
144 {
145 throw ActionParseError{ActionBase::getName(),
146 "Missing required delta value"};
147 }
148 _delta = jsonObj["delta"].get<uint64_t>();
149}
150
151} // namespace phosphor::fan::control::json