blob: 7877e4565a90a090eb6273af9fc9857e4b3df9be [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 <nlohmann/json.hpp>
19#include <phosphor-logging/log.hpp>
Matt Spinler8c0a63f2018-03-27 12:27:23 -050020
Patrick Williams6a2b8952023-05-10 07:50:35 -050021#include <experimental/filesystem>
22#include <fstream>
23
Matt Spinler8c0a63f2018-03-27 12:27:23 -050024namespace ibm
25{
26namespace logging
27{
28namespace policy
29{
30
31namespace fs = std::experimental::filesystem;
32using namespace phosphor::logging;
33
34Table::Table(const std::string& jsonFile)
35{
36 if (fs::exists(jsonFile))
37 {
38 load(jsonFile);
39 }
40 else
41 {
42 log<level::INFO>("Policy table JSON file does not exist",
Matt Spinler259e7272018-03-29 10:57:17 -050043 entry("FILE=%s", jsonFile.c_str()));
Matt Spinler8c0a63f2018-03-27 12:27:23 -050044 }
45}
46
47void Table::load(const std::string& jsonFile)
48{
49 try
50 {
51 std::ifstream file{jsonFile};
52
53 auto json = nlohmann::json::parse(file, nullptr, true);
54
55 for (const auto& policy : json)
56 {
57 DetailsList detailsList;
58
59 for (const auto& details : policy["dtls"])
60 {
61 Details d;
62 d.modifier = details["mod"];
63 d.msg = details["msg"];
64 d.ceid = details["CEID"];
65 detailsList.emplace_back(std::move(d));
66 }
67 policies.emplace(policy["err"], std::move(detailsList));
68 }
69
70 loaded = true;
71 }
Patrick Williamsbf9ea8a2021-10-06 13:10:21 -050072 catch (const std::exception& e)
Matt Spinler8c0a63f2018-03-27 12:27:23 -050073 {
74 log<level::ERR>("Failed loading policy table json file",
Matt Spinler259e7272018-03-29 10:57:17 -050075 entry("FILE=%s", jsonFile.c_str()),
76 entry("ERROR=%s", e.what()));
Matt Spinler8c0a63f2018-03-27 12:27:23 -050077 loaded = false;
78 }
79}
80
Matt Spinlerc57aa4b2018-09-28 10:32:24 -050081FindResult Table::find(const std::string& error,
82 const std::string& modifier) const
Matt Spinler3c9e3012018-03-27 12:36:53 -050083{
Matt Spinler259e7272018-03-29 10:57:17 -050084 // First find the entry based on the error, and then find which
85 // underlying details object it is with the help of the modifier.
Matt Spinler3c9e3012018-03-27 12:36:53 -050086
87 auto policy = policies.find(error);
88
89 if (policy != policies.end())
90 {
Matt Spinler200ead22018-04-24 11:50:51 -050091 // If there is no exact modifier match, then look for an entry with
92 // an empty modifier - it is the catch-all for that error.
Matt Spinler3c9e3012018-03-27 12:36:53 -050093 auto details = std::find_if(
Matt Spinler259e7272018-03-29 10:57:17 -050094 policy->second.begin(), policy->second.end(),
Matt Spinler200ead22018-04-24 11:50:51 -050095 [&modifier](const auto& d) { return modifier == d.modifier; });
96
97 if ((details == policy->second.end()) && !modifier.empty())
98 {
99 details =
100 std::find_if(policy->second.begin(), policy->second.end(),
101 [](const auto& d) { return d.modifier.empty(); });
102 }
Matt Spinler3c9e3012018-03-27 12:36:53 -0500103
104 if (details != policy->second.end())
105 {
106 return DetailsReference(*details);
107 }
108 }
109
110 return {};
111}
Matt Spinler66e07072018-09-12 10:36:14 -0500112} // namespace policy
113} // namespace logging
114} // namespace ibm