blob: a7b47b821095786de947f200e90afad96d3ae500 [file] [log] [blame]
Matthew Barth554ba472020-08-18 14:48:38 -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 */
16#pragma once
17
18#include <nlohmann/json.hpp>
19#include <phosphor-logging/log.hpp>
20
Matthew Bartha3a8cc52021-01-15 12:40:25 -060021#include <vector>
22
Matthew Barth554ba472020-08-18 14:48:38 -050023namespace phosphor::fan::control::json
24{
25
26using json = nlohmann::json;
27using namespace phosphor::logging;
28
Matthew Barthb584d812021-03-11 15:55:04 -060029using PropertyVariantType =
30 std::variant<bool, int32_t, int64_t, double, std::string>;
31
Matthew Barth554ba472020-08-18 14:48:38 -050032/**
Matthew Barth44ab7692021-03-26 11:40:10 -050033 * Configuration object key to uniquely map to the configuration object
34 * Pair constructed of:
35 * std::string = Configuration object's name
36 * std::vector<std::string> = List of profiles the configuration object
37 * is included in
38 */
39using configKey = std::pair<std::string, std::vector<std::string>>;
40
41/**
Matthew Barth554ba472020-08-18 14:48:38 -050042 * @class ConfigBase - Base configuration object
43 *
44 * Base class for fan control's JSON configuration objects.
45 */
46class ConfigBase
47{
48 public:
49 ConfigBase() = delete;
Matthew Barth554ba472020-08-18 14:48:38 -050050 ConfigBase(ConfigBase&&) = delete;
51 ConfigBase& operator=(const ConfigBase&) = delete;
52 ConfigBase& operator=(ConfigBase&&) = delete;
Mike Cappsb2e9a4f2022-06-13 10:15:42 -040053
Matthew Barth554ba472020-08-18 14:48:38 -050054 virtual ~ConfigBase() = default;
55
56 explicit ConfigBase(const json& jsonObj)
57 {
58 // Set the name of this configuration object
59 setName(jsonObj);
Matthew Barthbfd7e1b2021-02-04 14:25:47 -060060 if (jsonObj.contains("profiles"))
61 {
62 for (const auto& profile : jsonObj["profiles"])
63 {
64 _profiles.emplace_back(profile.get<std::string>());
65 }
66 }
Matthew Barth554ba472020-08-18 14:48:38 -050067 }
68
69 /**
Matthew Barthe5578602021-03-30 12:53:24 -050070 * Copy Constructor
71 * Creates a config base from another config base's originally parsed JSON
72 * object data
73 *
74 * @param[in] origObj - Original ConfigBase object to be created from
75 */
76 ConfigBase(const ConfigBase& origObj)
77 {
78 _name = origObj._name;
79 _profiles = origObj._profiles;
80 }
81
82 /**
Matthew Barth554ba472020-08-18 14:48:38 -050083 * @brief Get the configuration object's name
84 *
85 * @return Name of the configuration object
86 */
87 inline const std::string& getName() const
88 {
89 return _name;
90 }
91
Matthew Barthce957262020-08-27 10:35:57 -050092 /**
93 * @brief Get the configuration object's list of profiles
94 *
95 * Gets the list of profiles this configuration object belongs to if any
96 * are configured, otherwise an empty list of profiles results in the
97 * object always being included in the configuration.
98 *
99 * @return List of profiles the configuration object belongs to
100 */
101 inline const auto& getProfiles() const
102 {
103 return _profiles;
104 }
105
Matthew Barth9167c4d2020-08-24 13:17:43 -0500106 /**
107 * @brief Determines the data type of a JSON configured parameter that is
108 * used as a variant within the fan control application and returns the
109 * value as that variant.
110 * @details Retrieves a JSON object by the first derived data type that
111 * is not null. Expected data types should appear in a logical order of
112 * conversion. i.e.) uint and int could both be uint
113 *
114 * @param[in] object - A single JSON object
115 *
116 * @return A `PropertyVariantType` variant containing the JSON object's
117 * value
118 */
119 static const PropertyVariantType getJsonValue(const json& object)
120 {
121 if (auto boolPtr = object.get_ptr<const bool*>())
122 {
123 return *boolPtr;
124 }
125 if (auto intPtr = object.get_ptr<const int64_t*>())
126 {
127 return *intPtr;
128 }
129 if (auto doublePtr = object.get_ptr<const double*>())
130 {
131 return *doublePtr;
132 }
133 if (auto stringPtr = object.get_ptr<const std::string*>())
134 {
135 return *stringPtr;
136 }
137
138 log<level::ERR>(
139 "Unsupported data type for JSON object's value",
140 entry("JSON_ENTRY=%s", object.dump().c_str()),
141 entry("SUPPORTED_TYPES=%s", "{bool, int, double, string}"));
142 throw std::runtime_error(
143 "Unsupported data type for JSON object's value");
144 }
145
Matt Spinlerc2727902022-02-02 11:13:09 -0600146 protected:
Matthew Bartha3a8cc52021-01-15 12:40:25 -0600147 /* Name of the configuration object */
148 std::string _name;
149
Matthew Barthce957262020-08-27 10:35:57 -0500150 /**
151 * Profiles this configuration object belongs to (OPTIONAL).
152 * Otherwise always include this object in the configuration
153 * when no profiles are given
154 */
155 std::vector<std::string> _profiles;
156
Matthew Barth554ba472020-08-18 14:48:38 -0500157 private:
Matthew Barth554ba472020-08-18 14:48:38 -0500158 /**
159 * @brief Sets the configuration object's name from the given JSON
160 *
161 * @param[in] jsonObj - JSON to get configuration object's name from
162 */
163 inline void setName(const json& jsonObj)
164 {
165 if (!jsonObj.contains("name"))
166 {
167 // Log error on missing configuration object's name
168 log<level::ERR>("Missing required configuration object's name",
169 entry("JSON=%s", jsonObj.dump().c_str()));
170 throw std::runtime_error(
171 "Missing required configuration object's name");
172 }
173 _name = jsonObj["name"].get<std::string>();
174 }
175};
176
177} // namespace phosphor::fan::control::json