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 | */ |
| 16 | #include <experimental/filesystem> |
| 17 | #include <fstream> |
| 18 | #include <nlohmann/json.hpp> |
| 19 | #include <phosphor-logging/log.hpp> |
| 20 | #include "policy_table.hpp" |
| 21 | |
| 22 | namespace ibm |
| 23 | { |
| 24 | namespace logging |
| 25 | { |
| 26 | namespace policy |
| 27 | { |
| 28 | |
| 29 | namespace fs = std::experimental::filesystem; |
| 30 | using namespace phosphor::logging; |
| 31 | |
| 32 | Table::Table(const std::string& jsonFile) |
| 33 | { |
| 34 | if (fs::exists(jsonFile)) |
| 35 | { |
| 36 | load(jsonFile); |
| 37 | } |
| 38 | else |
| 39 | { |
| 40 | log<level::INFO>("Policy table JSON file does not exist", |
Matt Spinler | 259e727 | 2018-03-29 10:57:17 -0500 | [diff] [blame] | 41 | entry("FILE=%s", jsonFile.c_str())); |
Matt Spinler | 8c0a63f | 2018-03-27 12:27:23 -0500 | [diff] [blame] | 42 | } |
| 43 | } |
| 44 | |
| 45 | void Table::load(const std::string& jsonFile) |
| 46 | { |
| 47 | try |
| 48 | { |
| 49 | std::ifstream file{jsonFile}; |
| 50 | |
| 51 | auto json = nlohmann::json::parse(file, nullptr, true); |
| 52 | |
| 53 | for (const auto& policy : json) |
| 54 | { |
| 55 | DetailsList detailsList; |
| 56 | |
| 57 | for (const auto& details : policy["dtls"]) |
| 58 | { |
| 59 | Details d; |
| 60 | d.modifier = details["mod"]; |
| 61 | d.msg = details["msg"]; |
| 62 | d.ceid = details["CEID"]; |
| 63 | detailsList.emplace_back(std::move(d)); |
| 64 | } |
| 65 | policies.emplace(policy["err"], std::move(detailsList)); |
| 66 | } |
| 67 | |
| 68 | loaded = true; |
| 69 | } |
| 70 | catch (std::exception& e) |
| 71 | { |
| 72 | log<level::ERR>("Failed loading policy table json file", |
Matt Spinler | 259e727 | 2018-03-29 10:57:17 -0500 | [diff] [blame] | 73 | entry("FILE=%s", jsonFile.c_str()), |
| 74 | entry("ERROR=%s", e.what())); |
Matt Spinler | 8c0a63f | 2018-03-27 12:27:23 -0500 | [diff] [blame] | 75 | loaded = false; |
| 76 | } |
| 77 | } |
| 78 | |
Matt Spinler | 259e727 | 2018-03-29 10:57:17 -0500 | [diff] [blame] | 79 | optional_ns::optional<DetailsReference> |
| 80 | Table::find(const std::string& error, const std::string& modifier) const |
Matt Spinler | 3c9e301 | 2018-03-27 12:36:53 -0500 | [diff] [blame] | 81 | { |
Matt Spinler | 259e727 | 2018-03-29 10:57:17 -0500 | [diff] [blame] | 82 | // First find the entry based on the error, and then find which |
| 83 | // underlying details object it is with the help of the modifier. |
Matt Spinler | 3c9e301 | 2018-03-27 12:36:53 -0500 | [diff] [blame] | 84 | |
| 85 | auto policy = policies.find(error); |
| 86 | |
| 87 | if (policy != policies.end()) |
| 88 | { |
Matt Spinler | 200ead2 | 2018-04-24 11:50:51 -0500 | [diff] [blame^] | 89 | // If there is no exact modifier match, then look for an entry with |
| 90 | // an empty modifier - it is the catch-all for that error. |
Matt Spinler | 3c9e301 | 2018-03-27 12:36:53 -0500 | [diff] [blame] | 91 | auto details = std::find_if( |
Matt Spinler | 259e727 | 2018-03-29 10:57:17 -0500 | [diff] [blame] | 92 | policy->second.begin(), policy->second.end(), |
Matt Spinler | 200ead2 | 2018-04-24 11:50:51 -0500 | [diff] [blame^] | 93 | [&modifier](const auto& d) { return modifier == d.modifier; }); |
| 94 | |
| 95 | if ((details == policy->second.end()) && !modifier.empty()) |
| 96 | { |
| 97 | details = |
| 98 | std::find_if(policy->second.begin(), policy->second.end(), |
| 99 | [](const auto& d) { return d.modifier.empty(); }); |
| 100 | } |
Matt Spinler | 3c9e301 | 2018-03-27 12:36:53 -0500 | [diff] [blame] | 101 | |
| 102 | if (details != policy->second.end()) |
| 103 | { |
| 104 | return DetailsReference(*details); |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | return {}; |
| 109 | } |
Matt Spinler | 8c0a63f | 2018-03-27 12:27:23 -0500 | [diff] [blame] | 110 | } |
| 111 | } |
| 112 | } |