Matt Spinler | c09b8c8 | 2021-08-05 16:29:47 -0500 | [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 | |
| 17 | #include "modifier.hpp" |
| 18 | |
Matt Spinler | c272790 | 2022-02-02 11:13:09 -0600 | [diff] [blame] | 19 | #include "json/config_base.hpp" |
Matt Spinler | c09b8c8 | 2021-08-05 16:29:47 -0500 | [diff] [blame] | 20 | #include "json/manager.hpp" |
| 21 | |
Anwaar Hadi | 64b5ac2 | 2025-04-04 23:54:53 +0000 | [diff] [blame] | 22 | #include <phosphor-logging/lg2.hpp> |
Matt Spinler | c09b8c8 | 2021-08-05 16:29:47 -0500 | [diff] [blame] | 23 | |
| 24 | namespace phosphor::fan::control::json |
| 25 | { |
| 26 | |
| 27 | /** |
| 28 | * @brief Variant visitor to return a value of the template type specified. |
| 29 | */ |
| 30 | template <typename T> |
| 31 | struct ToTypeVisitor |
| 32 | { |
| 33 | template <typename U> |
| 34 | T operator()(const U& t) const |
| 35 | { |
| 36 | if constexpr (std::is_arithmetic_v<U> && std::is_arithmetic_v<T>) |
| 37 | { |
| 38 | return static_cast<T>(t); |
| 39 | } |
| 40 | throw std::invalid_argument( |
| 41 | "Non arithmetic type used in ToTypeVisitor"); |
| 42 | } |
| 43 | }; |
| 44 | |
| 45 | /** |
Matt Spinler | c272790 | 2022-02-02 11:13:09 -0600 | [diff] [blame] | 46 | * @brief Return a default value to use when the argument passed |
| 47 | * to LessThanOperator is out of range. |
| 48 | */ |
Patrick Williams | 4fa67aa | 2025-02-03 14:28:47 -0500 | [diff] [blame] | 49 | PropertyVariantType getDefaultValue( |
| 50 | const PropertyVariantType& val, |
| 51 | const std::optional<PropertyVariantType>& defaultValue) |
Matt Spinler | c272790 | 2022-02-02 11:13:09 -0600 | [diff] [blame] | 52 | { |
Matthew Barth | 6941fd7 | 2022-03-10 16:50:30 -0600 | [diff] [blame] | 53 | // When a default value is given, return that |
| 54 | if (defaultValue) |
| 55 | { |
| 56 | return *defaultValue; |
| 57 | } |
| 58 | |
Matt Spinler | c272790 | 2022-02-02 11:13:09 -0600 | [diff] [blame] | 59 | if (std::holds_alternative<bool>(val)) |
| 60 | { |
| 61 | return false; |
| 62 | } |
| 63 | else if (std::holds_alternative<std::string>(val)) |
| 64 | { |
| 65 | return std::string{}; |
| 66 | } |
| 67 | else if (std::holds_alternative<double>(val)) |
| 68 | { |
| 69 | return std::numeric_limits<double>::quiet_NaN(); |
| 70 | } |
| 71 | else if (std::holds_alternative<int32_t>(val)) |
| 72 | { |
| 73 | return std::numeric_limits<int32_t>::quiet_NaN(); |
| 74 | } |
| 75 | else if (std::holds_alternative<int64_t>(val)) |
| 76 | { |
| 77 | return std::numeric_limits<int64_t>::quiet_NaN(); |
| 78 | } |
| 79 | |
| 80 | throw std::runtime_error( |
| 81 | "Invalid variant type when determining default value"); |
| 82 | } |
| 83 | |
| 84 | /** |
Matt Spinler | c09b8c8 | 2021-08-05 16:29:47 -0500 | [diff] [blame] | 85 | * @brief Implements the minus operator to subtract two values. |
| 86 | * |
| 87 | * With strings values, A - B removes all occurrences of B in A. |
| 88 | * Throws if the type is a bool. |
| 89 | */ |
| 90 | struct MinusOperator : public Modifier::BaseOperator |
| 91 | { |
Matt Spinler | 9b06243 | 2023-01-26 14:38:50 -0600 | [diff] [blame] | 92 | explicit MinusOperator(const json& jsonObj) : |
Matthew Barth | 6941fd7 | 2022-03-10 16:50:30 -0600 | [diff] [blame] | 93 | arg(ConfigBase::getJsonValue(jsonObj["value"])) |
Matt Spinler | c272790 | 2022-02-02 11:13:09 -0600 | [diff] [blame] | 94 | {} |
| 95 | |
Matt Spinler | c09b8c8 | 2021-08-05 16:29:47 -0500 | [diff] [blame] | 96 | PropertyVariantType operator()(double val) override |
| 97 | { |
| 98 | return val - std::visit(ToTypeVisitor<double>(), arg); |
| 99 | } |
| 100 | |
| 101 | PropertyVariantType operator()(int32_t val) override |
| 102 | { |
| 103 | return val - std::visit(ToTypeVisitor<int32_t>(), arg); |
| 104 | } |
| 105 | |
| 106 | PropertyVariantType operator()(int64_t val) override |
| 107 | { |
| 108 | return val - std::visit(ToTypeVisitor<int64_t>(), arg); |
| 109 | } |
| 110 | |
| 111 | PropertyVariantType operator()(const std::string& val) override |
| 112 | { |
| 113 | // Remove all occurrences of arg from val. |
| 114 | auto value = val; |
| 115 | auto toRemove = std::get<std::string>(arg); |
| 116 | size_t pos; |
| 117 | while ((pos = value.find(toRemove)) != std::string::npos) |
| 118 | { |
| 119 | value.erase(pos, toRemove.size()); |
| 120 | } |
| 121 | |
| 122 | return value; |
| 123 | } |
| 124 | |
Mike Capps | b2e9a4f | 2022-06-13 10:15:42 -0400 | [diff] [blame] | 125 | PropertyVariantType operator()(bool) override |
Matt Spinler | c09b8c8 | 2021-08-05 16:29:47 -0500 | [diff] [blame] | 126 | { |
| 127 | throw std::runtime_error{ |
| 128 | "Bool not allowed as a 'minus' modifier value"}; |
| 129 | } |
| 130 | |
Matt Spinler | c09b8c8 | 2021-08-05 16:29:47 -0500 | [diff] [blame] | 131 | PropertyVariantType arg; |
| 132 | }; |
| 133 | |
Matt Spinler | c272790 | 2022-02-02 11:13:09 -0600 | [diff] [blame] | 134 | /** |
Matthew Barth | 6941fd7 | 2022-03-10 16:50:30 -0600 | [diff] [blame] | 135 | * @brief Implements an operator to return a value specified in the JSON that is |
| 136 | * chosen based on if the value passed into the operator is less than the lowest |
| 137 | * arg_value it is true for or the given `default_value` if not found to be less |
| 138 | * than any entries. |
Matt Spinler | c272790 | 2022-02-02 11:13:09 -0600 | [diff] [blame] | 139 | * |
| 140 | * "modifier": { |
| 141 | * "operator": "less_than", |
Matthew Barth | 6941fd7 | 2022-03-10 16:50:30 -0600 | [diff] [blame] | 142 | * "default_value": 1000, // OPTIONAL |
Matt Spinler | c272790 | 2022-02-02 11:13:09 -0600 | [diff] [blame] | 143 | * "value": [ |
| 144 | * { |
| 145 | * "arg_value": 30, // if value is less than 30 |
| 146 | * "parameter_value": 300 // then return 300 |
| 147 | * }, |
| 148 | * { |
| 149 | * "arg_value": 40, // else if value is less than 40 |
| 150 | * "parameter_value": 400 // then return 400 |
| 151 | * }, |
| 152 | * ] |
| 153 | * } |
| 154 | * |
Matthew Barth | 6941fd7 | 2022-03-10 16:50:30 -0600 | [diff] [blame] | 155 | * If the value passed in is higher than the highest arg_value, it returns a |
| 156 | * default value this is the `default_value` given or based on the data type of |
| 157 | * parameter_value. |
Matt Spinler | c272790 | 2022-02-02 11:13:09 -0600 | [diff] [blame] | 158 | */ |
| 159 | struct LessThanOperator : public Modifier::BaseOperator |
| 160 | { |
Matt Spinler | 9b06243 | 2023-01-26 14:38:50 -0600 | [diff] [blame] | 161 | explicit LessThanOperator(const json& jsonObj) |
Matt Spinler | c272790 | 2022-02-02 11:13:09 -0600 | [diff] [blame] | 162 | { |
Matthew Barth | 6941fd7 | 2022-03-10 16:50:30 -0600 | [diff] [blame] | 163 | const auto& valueArray = jsonObj["value"]; |
Matt Spinler | c272790 | 2022-02-02 11:13:09 -0600 | [diff] [blame] | 164 | if (!valueArray.is_array()) |
| 165 | { |
Anwaar Hadi | 64b5ac2 | 2025-04-04 23:54:53 +0000 | [diff] [blame] | 166 | lg2::error("Invalid JSON data for less_than config: {VALUE_ARRAY}", |
| 167 | "VALUE_ARRAY", valueArray.dump()); |
Matt Spinler | c272790 | 2022-02-02 11:13:09 -0600 | [diff] [blame] | 168 | throw std::invalid_argument("Invalid modifier JSON"); |
| 169 | } |
| 170 | |
| 171 | for (const auto& valueEntry : valueArray) |
| 172 | { |
| 173 | if (!valueEntry.contains("arg_value") || |
| 174 | !valueEntry.contains("parameter_value")) |
| 175 | { |
Anwaar Hadi | 64b5ac2 | 2025-04-04 23:54:53 +0000 | [diff] [blame] | 176 | lg2::error("Missing arg_value or parameter_value keys " |
| 177 | "in less_than config: {VALUE_ARRAY}", |
| 178 | "VALUE_ARRAY", valueArray.dump()); |
Matt Spinler | c272790 | 2022-02-02 11:13:09 -0600 | [diff] [blame] | 179 | throw std::invalid_argument("Invalid modifier JSON"); |
| 180 | } |
| 181 | |
| 182 | auto argVal = ConfigBase::getJsonValue(valueEntry.at("arg_value")); |
| 183 | |
| 184 | if (std::holds_alternative<bool>(argVal)) |
| 185 | { |
Anwaar Hadi | 64b5ac2 | 2025-04-04 23:54:53 +0000 | [diff] [blame] | 186 | lg2::error( |
| 187 | "Invalid data type in arg_value key in modifier JSON " |
| 188 | "config: {VALUE_ARRAY}", |
| 189 | "VALUE_ARRAY", valueArray.dump()); |
Matt Spinler | c272790 | 2022-02-02 11:13:09 -0600 | [diff] [blame] | 190 | throw std::invalid_argument("Invalid modifier JSON"); |
| 191 | } |
| 192 | |
| 193 | auto paramVal = |
| 194 | ConfigBase::getJsonValue(valueEntry.at("parameter_value")); |
| 195 | |
| 196 | rangeValues.emplace_back(argVal, paramVal); |
| 197 | } |
| 198 | |
| 199 | if (rangeValues.empty()) |
| 200 | { |
Anwaar Hadi | 64b5ac2 | 2025-04-04 23:54:53 +0000 | [diff] [blame] | 201 | lg2::error("No valid range values found in " |
| 202 | "modifier json: {VALUE_ARRAY}", |
| 203 | "VALUE_ARRAY", valueArray.dump()); |
Matt Spinler | c272790 | 2022-02-02 11:13:09 -0600 | [diff] [blame] | 204 | throw std::invalid_argument("Invalid modifier JSON"); |
| 205 | } |
Matthew Barth | 6941fd7 | 2022-03-10 16:50:30 -0600 | [diff] [blame] | 206 | |
| 207 | if (jsonObj.contains("default_value")) |
| 208 | { |
| 209 | defaultValue = ConfigBase::getJsonValue(jsonObj["default_value"]); |
| 210 | } |
Matt Spinler | c272790 | 2022-02-02 11:13:09 -0600 | [diff] [blame] | 211 | } |
| 212 | |
| 213 | PropertyVariantType operator()(double val) override |
| 214 | { |
| 215 | for (const auto& rangeValue : rangeValues) |
| 216 | { |
| 217 | if (val < std::visit(ToTypeVisitor<double>(), rangeValue.first)) |
| 218 | { |
| 219 | return rangeValue.second; |
| 220 | } |
| 221 | } |
| 222 | // Return a default value based on last entry type |
Matthew Barth | 6941fd7 | 2022-03-10 16:50:30 -0600 | [diff] [blame] | 223 | return getDefaultValue(rangeValues.back().second, defaultValue); |
Matt Spinler | c272790 | 2022-02-02 11:13:09 -0600 | [diff] [blame] | 224 | } |
| 225 | |
| 226 | PropertyVariantType operator()(int32_t val) override |
| 227 | { |
| 228 | for (const auto& rangeValue : rangeValues) |
| 229 | { |
| 230 | if (val < std::visit(ToTypeVisitor<int32_t>(), rangeValue.first)) |
| 231 | { |
| 232 | return rangeValue.second; |
| 233 | } |
| 234 | } |
Matthew Barth | 6941fd7 | 2022-03-10 16:50:30 -0600 | [diff] [blame] | 235 | return getDefaultValue(rangeValues.back().second, defaultValue); |
Matt Spinler | c272790 | 2022-02-02 11:13:09 -0600 | [diff] [blame] | 236 | } |
| 237 | |
| 238 | PropertyVariantType operator()(int64_t val) override |
| 239 | { |
| 240 | for (const auto& rangeValue : rangeValues) |
| 241 | { |
| 242 | if (val < std::visit(ToTypeVisitor<int64_t>(), rangeValue.first)) |
| 243 | { |
| 244 | return rangeValue.second; |
| 245 | } |
| 246 | } |
Matthew Barth | 6941fd7 | 2022-03-10 16:50:30 -0600 | [diff] [blame] | 247 | return getDefaultValue(rangeValues.back().second, defaultValue); |
Matt Spinler | c272790 | 2022-02-02 11:13:09 -0600 | [diff] [blame] | 248 | } |
| 249 | |
| 250 | PropertyVariantType operator()(const std::string& val) override |
| 251 | { |
| 252 | for (const auto& rangeValue : rangeValues) |
| 253 | { |
| 254 | if (val < |
| 255 | std::visit(ToTypeVisitor<std::string>(), rangeValue.first)) |
| 256 | { |
| 257 | return rangeValue.second; |
| 258 | } |
| 259 | } |
Matthew Barth | 6941fd7 | 2022-03-10 16:50:30 -0600 | [diff] [blame] | 260 | return getDefaultValue(rangeValues.back().second, defaultValue); |
Matt Spinler | c272790 | 2022-02-02 11:13:09 -0600 | [diff] [blame] | 261 | } |
| 262 | |
Mike Capps | b2e9a4f | 2022-06-13 10:15:42 -0400 | [diff] [blame] | 263 | PropertyVariantType operator()(bool) override |
Matt Spinler | c272790 | 2022-02-02 11:13:09 -0600 | [diff] [blame] | 264 | { |
| 265 | throw std::runtime_error{ |
| 266 | "Bool not allowed as a 'less_than' modifier value"}; |
| 267 | } |
| 268 | |
| 269 | std::vector<std::pair<PropertyVariantType, PropertyVariantType>> |
| 270 | rangeValues; |
Matthew Barth | 6941fd7 | 2022-03-10 16:50:30 -0600 | [diff] [blame] | 271 | std::optional<PropertyVariantType> defaultValue; |
Matt Spinler | c272790 | 2022-02-02 11:13:09 -0600 | [diff] [blame] | 272 | }; |
| 273 | |
Matt Spinler | c09b8c8 | 2021-08-05 16:29:47 -0500 | [diff] [blame] | 274 | Modifier::Modifier(const json& jsonObj) |
| 275 | { |
Matt Spinler | c09b8c8 | 2021-08-05 16:29:47 -0500 | [diff] [blame] | 276 | setOperator(jsonObj); |
| 277 | } |
| 278 | |
Matt Spinler | c09b8c8 | 2021-08-05 16:29:47 -0500 | [diff] [blame] | 279 | void Modifier::setOperator(const json& jsonObj) |
| 280 | { |
Matt Spinler | c272790 | 2022-02-02 11:13:09 -0600 | [diff] [blame] | 281 | if (!jsonObj.contains("operator") || !jsonObj.contains("value")) |
Matt Spinler | c09b8c8 | 2021-08-05 16:29:47 -0500 | [diff] [blame] | 282 | { |
Anwaar Hadi | 64b5ac2 | 2025-04-04 23:54:53 +0000 | [diff] [blame] | 283 | lg2::error( |
| 284 | "Modifier entry in JSON missing 'operator' or 'value': {JSON_OBJECT}", |
| 285 | "JSON_OBJECT", jsonObj.dump()); |
Matt Spinler | c09b8c8 | 2021-08-05 16:29:47 -0500 | [diff] [blame] | 286 | throw std::invalid_argument("Invalid modifier JSON"); |
| 287 | } |
| 288 | |
| 289 | auto op = jsonObj["operator"].get<std::string>(); |
| 290 | |
| 291 | if (op == "minus") |
| 292 | { |
Matthew Barth | 6941fd7 | 2022-03-10 16:50:30 -0600 | [diff] [blame] | 293 | _operator = std::make_unique<MinusOperator>(jsonObj); |
Matt Spinler | c272790 | 2022-02-02 11:13:09 -0600 | [diff] [blame] | 294 | } |
| 295 | else if (op == "less_than") |
| 296 | { |
Matthew Barth | 6941fd7 | 2022-03-10 16:50:30 -0600 | [diff] [blame] | 297 | _operator = std::make_unique<LessThanOperator>(jsonObj); |
Matt Spinler | c09b8c8 | 2021-08-05 16:29:47 -0500 | [diff] [blame] | 298 | } |
| 299 | else |
| 300 | { |
Anwaar Hadi | 64b5ac2 | 2025-04-04 23:54:53 +0000 | [diff] [blame] | 301 | lg2::error("Invalid operator in the modifier JSON: {JSON_OBJECT}", |
| 302 | "JSON_OBJECT", jsonObj.dump()); |
Matt Spinler | c09b8c8 | 2021-08-05 16:29:47 -0500 | [diff] [blame] | 303 | throw std::invalid_argument("Invalid operator in the modifier JSON"); |
| 304 | } |
| 305 | } |
| 306 | |
| 307 | PropertyVariantType Modifier::doOp(const PropertyVariantType& val) |
| 308 | { |
| 309 | return std::visit(*_operator, val); |
| 310 | } |
| 311 | |
| 312 | } // namespace phosphor::fan::control::json |