blob: 4a9d493967faabccae803e5cfa21089c1a6a9c07 [file] [log] [blame]
Matthew Barthfd05d642019-11-14 15:01:57 -06001/**
2 * Copyright © 2019 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#include <string>
Matthew Barth0961fae2019-11-15 09:00:27 -060017#include <filesystem>
18#include <fstream>
19#include <nlohmann/json.hpp>
20#include <phosphor-logging/log.hpp>
Matthew Barthfd05d642019-11-14 15:01:57 -060021
22#include "json_config.hpp"
23
24namespace phosphor
25{
26namespace fan
27{
28namespace presence
29{
30
Matthew Barth0961fae2019-11-15 09:00:27 -060031using json = nlohmann::json;
32namespace fs = std::filesystem;
33using namespace phosphor::logging;
34
Matthew Barthfd05d642019-11-14 15:01:57 -060035policies JsonConfig::_policies;
36
37JsonConfig::JsonConfig(const std::string& jsonFile)
38{
Matthew Barth0961fae2019-11-15 09:00:27 -060039 fs::path confFile{jsonFile};
40 std::ifstream file;
41 json jsonConf;
Matthew Barthfd05d642019-11-14 15:01:57 -060042
Matthew Barth0961fae2019-11-15 09:00:27 -060043 if (fs::exists(confFile))
44 {
45 file.open(confFile);
46 try
47 {
48 jsonConf = json::parse(file);
49 }
50 catch (std::exception& e)
51 {
52 log<level::ERR>("Failed to parse JSON config file",
53 entry("JSON_FILE=%s", jsonFile.c_str()),
54 entry("JSON_ERROR=%s", e.what()));
55 throw std::runtime_error("Failed to parse JSON config file");
56 }
57 }
58 else
59 {
60 log<level::ERR>("Unable to open JSON config file",
61 entry("JSON_FILE=%s", jsonFile.c_str()));
62 throw std::runtime_error("Unable to open JSON config file");
63 }
Matthew Barthfd05d642019-11-14 15:01:57 -060064}
65
66const policies& JsonConfig::get()
67{
68 return _policies;
69}
70
71} // namespace presence
72} // namespace fan
73} // namespace phosphor