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