Matthew Barth | 554ba47 | 2020-08-18 14:48:38 -0500 | [diff] [blame] | 1 | /** |
| 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 Barth | a3a8cc5 | 2021-01-15 12:40:25 -0600 | [diff] [blame] | 21 | #include <vector> |
| 22 | |
Matthew Barth | 554ba47 | 2020-08-18 14:48:38 -0500 | [diff] [blame] | 23 | namespace phosphor::fan::control::json |
| 24 | { |
| 25 | |
| 26 | using json = nlohmann::json; |
| 27 | using namespace phosphor::logging; |
| 28 | |
Matthew Barth | b584d81 | 2021-03-11 15:55:04 -0600 | [diff] [blame] | 29 | using PropertyVariantType = |
| 30 | std::variant<bool, int32_t, int64_t, double, std::string>; |
| 31 | |
Matthew Barth | 554ba47 | 2020-08-18 14:48:38 -0500 | [diff] [blame] | 32 | /** |
| 33 | * @class ConfigBase - Base configuration object |
| 34 | * |
| 35 | * Base class for fan control's JSON configuration objects. |
| 36 | */ |
| 37 | class 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 Barth | bfd7e1b | 2021-02-04 14:25:47 -0600 | [diff] [blame^] | 51 | if (jsonObj.contains("profiles")) |
| 52 | { |
| 53 | for (const auto& profile : jsonObj["profiles"]) |
| 54 | { |
| 55 | _profiles.emplace_back(profile.get<std::string>()); |
| 56 | } |
| 57 | } |
Matthew Barth | 554ba47 | 2020-08-18 14:48:38 -0500 | [diff] [blame] | 58 | } |
| 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 Barth | ce95726 | 2020-08-27 10:35:57 -0500 | [diff] [blame] | 70 | /** |
| 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 Barth | 9167c4d | 2020-08-24 13:17:43 -0500 | [diff] [blame] | 84 | 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 Barth | a3a8cc5 | 2021-01-15 12:40:25 -0600 | [diff] [blame] | 125 | /* Name of the configuration object */ |
| 126 | std::string _name; |
| 127 | |
Matthew Barth | ce95726 | 2020-08-27 10:35:57 -0500 | [diff] [blame] | 128 | /** |
| 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 Barth | 554ba47 | 2020-08-18 14:48:38 -0500 | [diff] [blame] | 135 | private: |
Matthew Barth | 554ba47 | 2020-08-18 14:48:38 -0500 | [diff] [blame] | 136 | /** |
| 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 |