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