blob: 3db6907a2ecfe3beb3d39d02f4917f5c91199f5f [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;
50 ConfigBase(const ConfigBase&) = delete;
51 ConfigBase(ConfigBase&&) = delete;
52 ConfigBase& operator=(const ConfigBase&) = delete;
53 ConfigBase& operator=(ConfigBase&&) = delete;
54 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 /**
70 * @brief Get the configuration object's name
71 *
72 * @return Name of the configuration object
73 */
74 inline const std::string& getName() const
75 {
76 return _name;
77 }
78
Matthew Barthce957262020-08-27 10:35:57 -050079 /**
80 * @brief Get the configuration object's list of profiles
81 *
82 * Gets the list of profiles this configuration object belongs to if any
83 * are configured, otherwise an empty list of profiles results in the
84 * object always being included in the configuration.
85 *
86 * @return List of profiles the configuration object belongs to
87 */
88 inline const auto& getProfiles() const
89 {
90 return _profiles;
91 }
92
Matthew Barth9167c4d2020-08-24 13:17:43 -050093 protected:
94 /**
95 * @brief Determines the data type of a JSON configured parameter that is
96 * used as a variant within the fan control application and returns the
97 * value as that variant.
98 * @details Retrieves a JSON object by the first derived data type that
99 * is not null. Expected data types should appear in a logical order of
100 * conversion. i.e.) uint and int could both be uint
101 *
102 * @param[in] object - A single JSON object
103 *
104 * @return A `PropertyVariantType` variant containing the JSON object's
105 * value
106 */
107 static const PropertyVariantType getJsonValue(const json& object)
108 {
109 if (auto boolPtr = object.get_ptr<const bool*>())
110 {
111 return *boolPtr;
112 }
113 if (auto intPtr = object.get_ptr<const int64_t*>())
114 {
115 return *intPtr;
116 }
117 if (auto doublePtr = object.get_ptr<const double*>())
118 {
119 return *doublePtr;
120 }
121 if (auto stringPtr = object.get_ptr<const std::string*>())
122 {
123 return *stringPtr;
124 }
125
126 log<level::ERR>(
127 "Unsupported data type for JSON object's value",
128 entry("JSON_ENTRY=%s", object.dump().c_str()),
129 entry("SUPPORTED_TYPES=%s", "{bool, int, double, string}"));
130 throw std::runtime_error(
131 "Unsupported data type for JSON object's value");
132 }
133
Matthew Bartha3a8cc52021-01-15 12:40:25 -0600134 /* Name of the configuration object */
135 std::string _name;
136
Matthew Barthce957262020-08-27 10:35:57 -0500137 /**
138 * Profiles this configuration object belongs to (OPTIONAL).
139 * Otherwise always include this object in the configuration
140 * when no profiles are given
141 */
142 std::vector<std::string> _profiles;
143
Matthew Barth554ba472020-08-18 14:48:38 -0500144 private:
Matthew Barth554ba472020-08-18 14:48:38 -0500145 /**
146 * @brief Sets the configuration object's name from the given JSON
147 *
148 * @param[in] jsonObj - JSON to get configuration object's name from
149 */
150 inline void setName(const json& jsonObj)
151 {
152 if (!jsonObj.contains("name"))
153 {
154 // Log error on missing configuration object's name
155 log<level::ERR>("Missing required configuration object's name",
156 entry("JSON=%s", jsonObj.dump().c_str()));
157 throw std::runtime_error(
158 "Missing required configuration object's name");
159 }
160 _name = jsonObj["name"].get<std::string>();
161 }
162};
163
164} // namespace phosphor::fan::control::json