blob: f77bc6e0c862dda31538d419138184554389c5bf [file] [log] [blame]
Matt Spinler8c0a63f2018-03-27 12:27:23 -05001/**
2 * Copyright © 2018 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 */
Matt Spinler66e07072018-09-12 10:36:14 -050016#include "policy_table.hpp"
17
Matt Spinler8c0a63f2018-03-27 12:27:23 -050018#include <experimental/filesystem>
19#include <fstream>
20#include <nlohmann/json.hpp>
21#include <phosphor-logging/log.hpp>
Matt Spinler8c0a63f2018-03-27 12:27:23 -050022
23namespace ibm
24{
25namespace logging
26{
27namespace policy
28{
29
30namespace fs = std::experimental::filesystem;
31using namespace phosphor::logging;
32
33Table::Table(const std::string& jsonFile)
34{
35 if (fs::exists(jsonFile))
36 {
37 load(jsonFile);
38 }
39 else
40 {
41 log<level::INFO>("Policy table JSON file does not exist",
Matt Spinler259e7272018-03-29 10:57:17 -050042 entry("FILE=%s", jsonFile.c_str()));
Matt Spinler8c0a63f2018-03-27 12:27:23 -050043 }
44}
45
46void Table::load(const std::string& jsonFile)
47{
48 try
49 {
50 std::ifstream file{jsonFile};
51
52 auto json = nlohmann::json::parse(file, nullptr, true);
53
54 for (const auto& policy : json)
55 {
56 DetailsList detailsList;
57
58 for (const auto& details : policy["dtls"])
59 {
60 Details d;
61 d.modifier = details["mod"];
62 d.msg = details["msg"];
63 d.ceid = details["CEID"];
64 detailsList.emplace_back(std::move(d));
65 }
66 policies.emplace(policy["err"], std::move(detailsList));
67 }
68
69 loaded = true;
70 }
Patrick Williamsbf9ea8a2021-10-06 13:10:21 -050071 catch (const std::exception& e)
Matt Spinler8c0a63f2018-03-27 12:27:23 -050072 {
73 log<level::ERR>("Failed loading policy table json file",
Matt Spinler259e7272018-03-29 10:57:17 -050074 entry("FILE=%s", jsonFile.c_str()),
75 entry("ERROR=%s", e.what()));
Matt Spinler8c0a63f2018-03-27 12:27:23 -050076 loaded = false;
77 }
78}
79
Matt Spinlerc57aa4b2018-09-28 10:32:24 -050080FindResult Table::find(const std::string& error,
81 const std::string& modifier) const
Matt Spinler3c9e3012018-03-27 12:36:53 -050082{
Matt Spinler259e7272018-03-29 10:57:17 -050083 // First find the entry based on the error, and then find which
84 // underlying details object it is with the help of the modifier.
Matt Spinler3c9e3012018-03-27 12:36:53 -050085
86 auto policy = policies.find(error);
87
88 if (policy != policies.end())
89 {
Matt Spinler200ead22018-04-24 11:50:51 -050090 // If there is no exact modifier match, then look for an entry with
91 // an empty modifier - it is the catch-all for that error.
Matt Spinler3c9e3012018-03-27 12:36:53 -050092 auto details = std::find_if(
Matt Spinler259e7272018-03-29 10:57:17 -050093 policy->second.begin(), policy->second.end(),
Matt Spinler200ead22018-04-24 11:50:51 -050094 [&modifier](const auto& d) { return modifier == d.modifier; });
95
96 if ((details == policy->second.end()) && !modifier.empty())
97 {
98 details =
99 std::find_if(policy->second.begin(), policy->second.end(),
100 [](const auto& d) { return d.modifier.empty(); });
101 }
Matt Spinler3c9e3012018-03-27 12:36:53 -0500102
103 if (details != policy->second.end())
104 {
105 return DetailsReference(*details);
106 }
107 }
108
109 return {};
110}
Matt Spinler66e07072018-09-12 10:36:14 -0500111} // namespace policy
112} // namespace logging
113} // namespace ibm