blob: 3184cb5c9cc197535c9237e687784c62fd3846bd [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
23namespace phosphor::fan::control::json
24{
25
26using json = nlohmann::json;
27using namespace phosphor::logging;
28
29/**
30 * @class ConfigBase - Base configuration object
31 *
32 * Base class for fan control's JSON configuration objects.
33 */
34class ConfigBase
35{
36 public:
37 ConfigBase() = delete;
38 ConfigBase(const ConfigBase&) = delete;
39 ConfigBase(ConfigBase&&) = delete;
40 ConfigBase& operator=(const ConfigBase&) = delete;
41 ConfigBase& operator=(ConfigBase&&) = delete;
42 virtual ~ConfigBase() = default;
43
44 explicit ConfigBase(const json& jsonObj)
45 {
46 // Set the name of this configuration object
47 setName(jsonObj);
48 }
49
50 /**
51 * @brief Get the configuration object's name
52 *
53 * @return Name of the configuration object
54 */
55 inline const std::string& getName() const
56 {
57 return _name;
58 }
59
Matthew Barth9167c4d2020-08-24 13:17:43 -050060 protected:
61 /**
62 * @brief Determines the data type of a JSON configured parameter that is
63 * used as a variant within the fan control application and returns the
64 * value as that variant.
65 * @details Retrieves a JSON object by the first derived data type that
66 * is not null. Expected data types should appear in a logical order of
67 * conversion. i.e.) uint and int could both be uint
68 *
69 * @param[in] object - A single JSON object
70 *
71 * @return A `PropertyVariantType` variant containing the JSON object's
72 * value
73 */
74 static const PropertyVariantType getJsonValue(const json& object)
75 {
76 if (auto boolPtr = object.get_ptr<const bool*>())
77 {
78 return *boolPtr;
79 }
80 if (auto intPtr = object.get_ptr<const int64_t*>())
81 {
82 return *intPtr;
83 }
84 if (auto doublePtr = object.get_ptr<const double*>())
85 {
86 return *doublePtr;
87 }
88 if (auto stringPtr = object.get_ptr<const std::string*>())
89 {
90 return *stringPtr;
91 }
92
93 log<level::ERR>(
94 "Unsupported data type for JSON object's value",
95 entry("JSON_ENTRY=%s", object.dump().c_str()),
96 entry("SUPPORTED_TYPES=%s", "{bool, int, double, string}"));
97 throw std::runtime_error(
98 "Unsupported data type for JSON object's value");
99 }
100
Matthew Barth554ba472020-08-18 14:48:38 -0500101 private:
102 /* Name of the configuration object */
103 std::string _name;
104
105 /**
106 * @brief Sets the configuration object's name from the given JSON
107 *
108 * @param[in] jsonObj - JSON to get configuration object's name from
109 */
110 inline void setName(const json& jsonObj)
111 {
112 if (!jsonObj.contains("name"))
113 {
114 // Log error on missing configuration object's name
115 log<level::ERR>("Missing required configuration object's name",
116 entry("JSON=%s", jsonObj.dump().c_str()));
117 throw std::runtime_error(
118 "Missing required configuration object's name");
119 }
120 _name = jsonObj["name"].get<std::string>();
121 }
122};
123
124} // namespace phosphor::fan::control::json