blob: 816fcdf0d91f3871045ae6a1c95de6bc16a09349 [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/**
33 * @class ConfigBase - Base configuration object
34 *
35 * Base class for fan control's JSON configuration objects.
36 */
37class ConfigBase
38{
39 public:
40 ConfigBase() = delete;
41 ConfigBase(const ConfigBase&) = delete;
42 ConfigBase(ConfigBase&&) = delete;
43 ConfigBase& operator=(const ConfigBase&) = delete;
44 ConfigBase& operator=(ConfigBase&&) = delete;
45 virtual ~ConfigBase() = default;
46
47 explicit ConfigBase(const json& jsonObj)
48 {
49 // Set the name of this configuration object
50 setName(jsonObj);
Matthew Barthbfd7e1b2021-02-04 14:25:47 -060051 if (jsonObj.contains("profiles"))
52 {
53 for (const auto& profile : jsonObj["profiles"])
54 {
55 _profiles.emplace_back(profile.get<std::string>());
56 }
57 }
Matthew Barth554ba472020-08-18 14:48:38 -050058 }
59
60 /**
61 * @brief Get the configuration object's name
62 *
63 * @return Name of the configuration object
64 */
65 inline const std::string& getName() const
66 {
67 return _name;
68 }
69
Matthew Barthce957262020-08-27 10:35:57 -050070 /**
71 * @brief Get the configuration object's list of profiles
72 *
73 * Gets the list of profiles this configuration object belongs to if any
74 * are configured, otherwise an empty list of profiles results in the
75 * object always being included in the configuration.
76 *
77 * @return List of profiles the configuration object belongs to
78 */
79 inline const auto& getProfiles() const
80 {
81 return _profiles;
82 }
83
Matthew Barth9167c4d2020-08-24 13:17:43 -050084 protected:
85 /**
86 * @brief Determines the data type of a JSON configured parameter that is
87 * used as a variant within the fan control application and returns the
88 * value as that variant.
89 * @details Retrieves a JSON object by the first derived data type that
90 * is not null. Expected data types should appear in a logical order of
91 * conversion. i.e.) uint and int could both be uint
92 *
93 * @param[in] object - A single JSON object
94 *
95 * @return A `PropertyVariantType` variant containing the JSON object's
96 * value
97 */
98 static const PropertyVariantType getJsonValue(const json& object)
99 {
100 if (auto boolPtr = object.get_ptr<const bool*>())
101 {
102 return *boolPtr;
103 }
104 if (auto intPtr = object.get_ptr<const int64_t*>())
105 {
106 return *intPtr;
107 }
108 if (auto doublePtr = object.get_ptr<const double*>())
109 {
110 return *doublePtr;
111 }
112 if (auto stringPtr = object.get_ptr<const std::string*>())
113 {
114 return *stringPtr;
115 }
116
117 log<level::ERR>(
118 "Unsupported data type for JSON object's value",
119 entry("JSON_ENTRY=%s", object.dump().c_str()),
120 entry("SUPPORTED_TYPES=%s", "{bool, int, double, string}"));
121 throw std::runtime_error(
122 "Unsupported data type for JSON object's value");
123 }
124
Matthew Bartha3a8cc52021-01-15 12:40:25 -0600125 /* Name of the configuration object */
126 std::string _name;
127
Matthew Barthce957262020-08-27 10:35:57 -0500128 /**
129 * Profiles this configuration object belongs to (OPTIONAL).
130 * Otherwise always include this object in the configuration
131 * when no profiles are given
132 */
133 std::vector<std::string> _profiles;
134
Matthew Barth554ba472020-08-18 14:48:38 -0500135 private:
Matthew Barth554ba472020-08-18 14:48:38 -0500136 /**
137 * @brief Sets the configuration object's name from the given JSON
138 *
139 * @param[in] jsonObj - JSON to get configuration object's name from
140 */
141 inline void setName(const json& jsonObj)
142 {
143 if (!jsonObj.contains("name"))
144 {
145 // Log error on missing configuration object's name
146 log<level::ERR>("Missing required configuration object's name",
147 entry("JSON=%s", jsonObj.dump().c_str()));
148 throw std::runtime_error(
149 "Missing required configuration object's name");
150 }
151 _name = jsonObj["name"].get<std::string>();
152 }
153};
154
155} // namespace phosphor::fan::control::json