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