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