Matthew Barth | 167d2dd | 2020-08-04 12:19:16 -0500 | [diff] [blame] | 1 | /** |
| 2 | * Copyright © 2020 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 | */ |
Matthew Barth | a56135b | 2020-08-18 14:53:50 -0500 | [diff] [blame] | 16 | #include "profile.hpp" |
Matthew Barth | 167d2dd | 2020-08-04 12:19:16 -0500 | [diff] [blame] | 17 | |
Matthew Barth | 9167c4d | 2020-08-24 13:17:43 -0500 | [diff] [blame] | 18 | #include "sdbusplus.hpp" |
| 19 | |
Matthew Barth | a56135b | 2020-08-18 14:53:50 -0500 | [diff] [blame] | 20 | #include <nlohmann/json.hpp> |
Anwaar Hadi | 64b5ac2 | 2025-04-04 23:54:53 +0000 | [diff] [blame] | 21 | #include <phosphor-logging/lg2.hpp> |
Matthew Barth | 167d2dd | 2020-08-04 12:19:16 -0500 | [diff] [blame] | 22 | |
Matthew Barth | 9167c4d | 2020-08-24 13:17:43 -0500 | [diff] [blame] | 23 | #include <algorithm> |
| 24 | #include <iterator> |
| 25 | #include <numeric> |
| 26 | |
Matthew Barth | 167d2dd | 2020-08-04 12:19:16 -0500 | [diff] [blame] | 27 | namespace phosphor::fan::control::json |
| 28 | { |
| 29 | |
Matthew Barth | a56135b | 2020-08-18 14:53:50 -0500 | [diff] [blame] | 30 | using json = nlohmann::json; |
Matthew Barth | a56135b | 2020-08-18 14:53:50 -0500 | [diff] [blame] | 31 | |
Matthew Barth | 9167c4d | 2020-08-24 13:17:43 -0500 | [diff] [blame] | 32 | // String key must be in all lowercase for method lookup |
| 33 | const std::map<std::string, methodHandler> Profile::_methods = { |
| 34 | {"all_of", Profile::allOf}}; |
| 35 | |
Matthew Barth | 391ade0 | 2021-01-15 14:33:21 -0600 | [diff] [blame] | 36 | Profile::Profile(const json& jsonObj) : ConfigBase(jsonObj), _active(false) |
Matthew Barth | 9167c4d | 2020-08-24 13:17:43 -0500 | [diff] [blame] | 37 | { |
| 38 | setActive(jsonObj); |
| 39 | } |
| 40 | |
| 41 | void Profile::setActive(const json& jsonObj) |
| 42 | { |
| 43 | if (!jsonObj.contains("method") || !jsonObj["method"].contains("name")) |
| 44 | { |
| 45 | // Log error on missing profile method |
Anwaar Hadi | 64b5ac2 | 2025-04-04 23:54:53 +0000 | [diff] [blame] | 46 | lg2::error("Missing required profile method", "JSON", jsonObj.dump()); |
Matthew Barth | 9167c4d | 2020-08-24 13:17:43 -0500 | [diff] [blame] | 47 | throw std::runtime_error("Missing required profile method"); |
| 48 | } |
| 49 | // The method to use in determining if the profile is active |
| 50 | auto method = jsonObj["method"]["name"].get<std::string>(); |
| 51 | std::transform(method.begin(), method.end(), method.begin(), tolower); |
| 52 | auto handler = _methods.find(method); |
| 53 | if (handler != _methods.end()) |
| 54 | { |
| 55 | // Call method for determining profile's active state |
| 56 | _active = handler->second(jsonObj["method"]); |
| 57 | } |
| 58 | else |
| 59 | { |
| 60 | // Construct list of available methods |
Patrick Williams | dfddd64 | 2024-08-16 15:21:51 -0400 | [diff] [blame] | 61 | auto methods = std::accumulate( |
| 62 | std::next(_methods.begin()), _methods.end(), |
| 63 | _methods.begin()->first, [](auto list, auto method) { |
| 64 | return std::move(list) + ", " + method.first; |
| 65 | }); |
Anwaar Hadi | 64b5ac2 | 2025-04-04 23:54:53 +0000 | [diff] [blame] | 66 | lg2::error( |
| 67 | "Configured method not available. Available methods are {METHODS_AVAILABLE}", |
| 68 | "METHODS_AVAILABLE", methods, "JSON", jsonObj["method"].dump()); |
Matthew Barth | 9167c4d | 2020-08-24 13:17:43 -0500 | [diff] [blame] | 69 | } |
| 70 | } |
| 71 | |
| 72 | bool Profile::allOf(const json& method) |
| 73 | { |
| 74 | if (!method.contains("properties")) |
| 75 | { |
Anwaar Hadi | 64b5ac2 | 2025-04-04 23:54:53 +0000 | [diff] [blame] | 76 | lg2::error("Missing required all_of method properties list", "JSON", |
| 77 | method.dump()); |
Matthew Barth | 9167c4d | 2020-08-24 13:17:43 -0500 | [diff] [blame] | 78 | throw std::runtime_error( |
| 79 | "Missing required all_of method properties list"); |
| 80 | } |
| 81 | |
Patrick Williams | dfddd64 | 2024-08-16 15:21:51 -0400 | [diff] [blame] | 82 | return std::all_of( |
| 83 | method["properties"].begin(), method["properties"].end(), |
| 84 | [](const json& obj) { |
| 85 | if (!obj.contains("path") || !obj.contains("interface") || |
| 86 | !obj.contains("property") || !obj.contains("value")) |
| 87 | { |
Anwaar Hadi | 64b5ac2 | 2025-04-04 23:54:53 +0000 | [diff] [blame] | 88 | lg2::error("Missing required all_of method property parameters", |
| 89 | "JSON", obj.dump()); |
Patrick Williams | dfddd64 | 2024-08-16 15:21:51 -0400 | [diff] [blame] | 90 | throw std::runtime_error( |
| 91 | "Missing required all_of method parameters"); |
| 92 | } |
| 93 | auto variant = |
| 94 | util::SDBusPlus::getPropertyVariant<PropertyVariantType>( |
| 95 | obj["path"].get<std::string>(), |
| 96 | obj["interface"].get<std::string>(), |
| 97 | obj["property"].get<std::string>()); |
Matthew Barth | 9167c4d | 2020-08-24 13:17:43 -0500 | [diff] [blame] | 98 | |
Patrick Williams | dfddd64 | 2024-08-16 15:21:51 -0400 | [diff] [blame] | 99 | return getJsonValue(obj["value"]) == variant; |
| 100 | }); |
Matthew Barth | 9167c4d | 2020-08-24 13:17:43 -0500 | [diff] [blame] | 101 | } |
Matthew Barth | 167d2dd | 2020-08-04 12:19:16 -0500 | [diff] [blame] | 102 | |
| 103 | } // namespace phosphor::fan::control::json |