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