blob: 36a4b2c458eb42fff85db6d528e1ef576ffdb79b [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
21namespace phosphor::fan::control::json
22{
23
24using json = nlohmann::json;
25using namespace phosphor::logging;
26
27/**
28 * @class ConfigBase - Base configuration object
29 *
30 * Base class for fan control's JSON configuration objects.
31 */
32class ConfigBase
33{
34 public:
35 ConfigBase() = delete;
36 ConfigBase(const ConfigBase&) = delete;
37 ConfigBase(ConfigBase&&) = delete;
38 ConfigBase& operator=(const ConfigBase&) = delete;
39 ConfigBase& operator=(ConfigBase&&) = delete;
40 virtual ~ConfigBase() = default;
41
42 explicit ConfigBase(const json& jsonObj)
43 {
44 // Set the name of this configuration object
45 setName(jsonObj);
46 }
47
48 /**
49 * @brief Get the configuration object's name
50 *
51 * @return Name of the configuration object
52 */
53 inline const std::string& getName() const
54 {
55 return _name;
56 }
57
58 private:
59 /* Name of the configuration object */
60 std::string _name;
61
62 /**
63 * @brief Sets the configuration object's name from the given JSON
64 *
65 * @param[in] jsonObj - JSON to get configuration object's name from
66 */
67 inline void setName(const json& jsonObj)
68 {
69 if (!jsonObj.contains("name"))
70 {
71 // Log error on missing configuration object's name
72 log<level::ERR>("Missing required configuration object's name",
73 entry("JSON=%s", jsonObj.dump().c_str()));
74 throw std::runtime_error(
75 "Missing required configuration object's name");
76 }
77 _name = jsonObj["name"].get<std::string>();
78 }
79};
80
81} // namespace phosphor::fan::control::json