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