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