blob: c56307f6fddf9888e13128398098666aa96e6bbb [file] [log] [blame]
Matthew Barth167d2dd2020-08-04 12:19:16 -05001/**
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 Bartha56135b2020-08-18 14:53:50 -050016#include "profile.hpp"
Matthew Barth167d2dd2020-08-04 12:19:16 -050017
Matthew Barth9167c4d2020-08-24 13:17:43 -050018#include "sdbusplus.hpp"
19
Matthew Bartha56135b2020-08-18 14:53:50 -050020#include <nlohmann/json.hpp>
Anwaar Hadi64b5ac22025-04-04 23:54:53 +000021#include <phosphor-logging/lg2.hpp>
Matthew Barth167d2dd2020-08-04 12:19:16 -050022
Matthew Barth9167c4d2020-08-24 13:17:43 -050023#include <algorithm>
24#include <iterator>
25#include <numeric>
26
Matthew Barth167d2dd2020-08-04 12:19:16 -050027namespace phosphor::fan::control::json
28{
29
Matthew Bartha56135b2020-08-18 14:53:50 -050030using json = nlohmann::json;
Matthew Bartha56135b2020-08-18 14:53:50 -050031
Matthew Barth9167c4d2020-08-24 13:17:43 -050032// String key must be in all lowercase for method lookup
33const std::map<std::string, methodHandler> Profile::_methods = {
34 {"all_of", Profile::allOf}};
35
Matthew Barth391ade02021-01-15 14:33:21 -060036Profile::Profile(const json& jsonObj) : ConfigBase(jsonObj), _active(false)
Matthew Barth9167c4d2020-08-24 13:17:43 -050037{
38 setActive(jsonObj);
39}
40
41void Profile::setActive(const json& jsonObj)
42{
43 if (!jsonObj.contains("method") || !jsonObj["method"].contains("name"))
44 {
45 // Log error on missing profile method
Anwaar Hadi64b5ac22025-04-04 23:54:53 +000046 lg2::error("Missing required profile method", "JSON", jsonObj.dump());
Matthew Barth9167c4d2020-08-24 13:17:43 -050047 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 Williamsdfddd642024-08-16 15:21:51 -040061 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 Hadi64b5ac22025-04-04 23:54:53 +000066 lg2::error(
67 "Configured method not available. Available methods are {METHODS_AVAILABLE}",
68 "METHODS_AVAILABLE", methods, "JSON", jsonObj["method"].dump());
Matthew Barth9167c4d2020-08-24 13:17:43 -050069 }
70}
71
72bool Profile::allOf(const json& method)
73{
74 if (!method.contains("properties"))
75 {
Anwaar Hadi64b5ac22025-04-04 23:54:53 +000076 lg2::error("Missing required all_of method properties list", "JSON",
77 method.dump());
Matthew Barth9167c4d2020-08-24 13:17:43 -050078 throw std::runtime_error(
79 "Missing required all_of method properties list");
80 }
81
Patrick Williamsdfddd642024-08-16 15:21:51 -040082 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 Hadi64b5ac22025-04-04 23:54:53 +000088 lg2::error("Missing required all_of method property parameters",
89 "JSON", obj.dump());
Patrick Williamsdfddd642024-08-16 15:21:51 -040090 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 Barth9167c4d2020-08-24 13:17:43 -050098
Patrick Williamsdfddd642024-08-16 15:21:51 -040099 return getJsonValue(obj["value"]) == variant;
100 });
Matthew Barth9167c4d2020-08-24 13:17:43 -0500101}
Matthew Barth167d2dd2020-08-04 12:19:16 -0500102
103} // namespace phosphor::fan::control::json