Matt Spinler | 8c0a63f | 2018-03-27 12:27:23 -0500 | [diff] [blame] | 1 | /** |
| 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 Spinler | 66e0707 | 2018-09-12 10:36:14 -0500 | [diff] [blame] | 16 | #include "policy_table.hpp" |
| 17 | |
Matt Spinler | 8c0a63f | 2018-03-27 12:27:23 -0500 | [diff] [blame] | 18 | #include <nlohmann/json.hpp> |
| 19 | #include <phosphor-logging/log.hpp> |
Matt Spinler | 8c0a63f | 2018-03-27 12:27:23 -0500 | [diff] [blame] | 20 | |
Patrick Williams | 6a2b895 | 2023-05-10 07:50:35 -0500 | [diff] [blame^] | 21 | #include <experimental/filesystem> |
| 22 | #include <fstream> |
| 23 | |
Matt Spinler | 8c0a63f | 2018-03-27 12:27:23 -0500 | [diff] [blame] | 24 | namespace ibm |
| 25 | { |
| 26 | namespace logging |
| 27 | { |
| 28 | namespace policy |
| 29 | { |
| 30 | |
| 31 | namespace fs = std::experimental::filesystem; |
| 32 | using namespace phosphor::logging; |
| 33 | |
| 34 | Table::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 Spinler | 259e727 | 2018-03-29 10:57:17 -0500 | [diff] [blame] | 43 | entry("FILE=%s", jsonFile.c_str())); |
Matt Spinler | 8c0a63f | 2018-03-27 12:27:23 -0500 | [diff] [blame] | 44 | } |
| 45 | } |
| 46 | |
| 47 | void 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 Williams | bf9ea8a | 2021-10-06 13:10:21 -0500 | [diff] [blame] | 72 | catch (const std::exception& e) |
Matt Spinler | 8c0a63f | 2018-03-27 12:27:23 -0500 | [diff] [blame] | 73 | { |
| 74 | log<level::ERR>("Failed loading policy table json file", |
Matt Spinler | 259e727 | 2018-03-29 10:57:17 -0500 | [diff] [blame] | 75 | entry("FILE=%s", jsonFile.c_str()), |
| 76 | entry("ERROR=%s", e.what())); |
Matt Spinler | 8c0a63f | 2018-03-27 12:27:23 -0500 | [diff] [blame] | 77 | loaded = false; |
| 78 | } |
| 79 | } |
| 80 | |
Matt Spinler | c57aa4b | 2018-09-28 10:32:24 -0500 | [diff] [blame] | 81 | FindResult Table::find(const std::string& error, |
| 82 | const std::string& modifier) const |
Matt Spinler | 3c9e301 | 2018-03-27 12:36:53 -0500 | [diff] [blame] | 83 | { |
Matt Spinler | 259e727 | 2018-03-29 10:57:17 -0500 | [diff] [blame] | 84 | // 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 Spinler | 3c9e301 | 2018-03-27 12:36:53 -0500 | [diff] [blame] | 86 | |
| 87 | auto policy = policies.find(error); |
| 88 | |
| 89 | if (policy != policies.end()) |
| 90 | { |
Matt Spinler | 200ead2 | 2018-04-24 11:50:51 -0500 | [diff] [blame] | 91 | // 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 Spinler | 3c9e301 | 2018-03-27 12:36:53 -0500 | [diff] [blame] | 93 | auto details = std::find_if( |
Matt Spinler | 259e727 | 2018-03-29 10:57:17 -0500 | [diff] [blame] | 94 | policy->second.begin(), policy->second.end(), |
Matt Spinler | 200ead2 | 2018-04-24 11:50:51 -0500 | [diff] [blame] | 95 | [&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 Spinler | 3c9e301 | 2018-03-27 12:36:53 -0500 | [diff] [blame] | 103 | |
| 104 | if (details != policy->second.end()) |
| 105 | { |
| 106 | return DetailsReference(*details); |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | return {}; |
| 111 | } |
Matt Spinler | 66e0707 | 2018-09-12 10:36:14 -0500 | [diff] [blame] | 112 | } // namespace policy |
| 113 | } // namespace logging |
| 114 | } // namespace ibm |