Zane Shelley | e5411f0 | 2021-08-04 22:41:35 -0500 | [diff] [blame] | 1 | #include <analyzer/service_data.hpp> |
Zane Shelley | 869d9bb | 2021-08-05 10:46:47 -0500 | [diff] [blame^] | 2 | #include <hei_main.hpp> |
| 3 | #include <nlohmann/json.hpp> |
| 4 | #include <util/data_file.hpp> |
| 5 | #include <util/trace.hpp> |
| 6 | |
| 7 | #include <filesystem> |
| 8 | #include <fstream> |
| 9 | #include <string> |
| 10 | |
| 11 | namespace fs = std::filesystem; |
Zane Shelley | e5411f0 | 2021-08-04 22:41:35 -0500 | [diff] [blame] | 12 | |
| 13 | namespace analyzer |
| 14 | { |
| 15 | |
| 16 | //------------------------------------------------------------------------------ |
| 17 | |
Zane Shelley | 869d9bb | 2021-08-05 10:46:47 -0500 | [diff] [blame^] | 18 | // Returns the RAS data schema file. Will throw exceptions if the file does not |
| 19 | // exist, is unreadable, or is malformed JSON. |
| 20 | nlohmann::json __getSchemaFile() |
| 21 | { |
| 22 | // Search all the files in the package `schema` subdirectory and find the |
| 23 | // file named `ras-data-schema.json`. |
| 24 | fs::path dirPath{PACKAGE_DIR "schema"}; |
| 25 | std::vector<fs::path> files; |
| 26 | util::findFiles(dirPath, R"(ras-data-schema\.json)", files); |
| 27 | assert(1 == files.size()); // Should be one, and only one, file. |
| 28 | |
| 29 | // Trace the file for debug. |
| 30 | trace::inf("File found: path=%s", files.front().string().c_str()); |
| 31 | |
| 32 | // Open the file. |
| 33 | std::ifstream file{files.front()}; |
| 34 | assert(file.good()); // The file must be readable. |
| 35 | |
| 36 | // Parse and return the JSON. Note that this will throw an exception if the |
| 37 | // file is not properly formatted. |
| 38 | return nlohmann::json::parse(file); |
| 39 | } |
| 40 | |
| 41 | //------------------------------------------------------------------------------ |
| 42 | |
| 43 | void __getDataFiles(std::map<libhei::ChipType_t, nlohmann::json>& o_files) |
| 44 | { |
| 45 | o_files.clear(); // initially empty |
| 46 | |
| 47 | // Get the schema document for the data files. |
| 48 | const auto schema = __getSchemaFile(); |
| 49 | |
| 50 | // Get all JSON files in the package `data` subdirectory. |
| 51 | fs::path dirPath{PACKAGE_DIR "ras-data"}; |
| 52 | std::vector<fs::path> files; |
| 53 | util::findFiles(dirPath, R"(.*\.json)", files); |
| 54 | |
| 55 | for (const auto& path : files) |
| 56 | { |
| 57 | // Trace each data file for debug. |
| 58 | trace::inf("File found: path=%s", path.string().c_str()); |
| 59 | |
| 60 | // Open the file. |
| 61 | std::ifstream file{path}; |
| 62 | assert(file.good()); // The file must be readable. |
| 63 | |
| 64 | // Parse the JSON. Note that this will throw an exception if the |
| 65 | // file is not properly formatted. |
| 66 | const auto data = nlohmann::json::parse(file); |
| 67 | |
| 68 | // Validate the data against the schema. |
| 69 | assert(util::validateJson(schema, data)); |
| 70 | |
| 71 | // Get the chip model/EC level from the data. The value is currently |
| 72 | // stored as a string representation of the hex value. So it will have |
| 73 | // to be converted to an integer. |
| 74 | libhei::ChipType_t chipType = |
| 75 | std::stoul(data.at("model_ec").get<std::string>(), 0, 16); |
| 76 | |
| 77 | // So far, so good. Add the entry. |
| 78 | auto ret = o_files.emplace(chipType, data); |
| 79 | assert(ret.second); // Should not have duplicate entries |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | //------------------------------------------------------------------------------ |
| 84 | |
Zane Shelley | e5411f0 | 2021-08-04 22:41:35 -0500 | [diff] [blame] | 85 | void applyRasActions(ServiceData& io_servData) |
| 86 | { |
Zane Shelley | 869d9bb | 2021-08-05 10:46:47 -0500 | [diff] [blame^] | 87 | // Find all of the existing chip data files. |
| 88 | std::map<libhei::ChipType_t, nlohmann::json> files; |
| 89 | __getDataFiles(files); |
| 90 | |
| 91 | // Get the RAS data file for this signature's chip type. |
| 92 | auto rootCause = io_servData.getRootCause(); |
| 93 | auto rasData_itr = files.find(rootCause.getChip().getType()); |
| 94 | assert(files.end() != rasData_itr); // the data file must exist |
| 95 | |
Zane Shelley | e5411f0 | 2021-08-04 22:41:35 -0500 | [diff] [blame] | 96 | // TODO: finish implementing |
| 97 | |
| 98 | // The default action is to callout level 2 support. |
| 99 | io_servData.addCallout(std::make_shared<ProcedureCallout>( |
| 100 | ProcedureCallout::NEXTLVL, Callout::Priority::HIGH)); |
| 101 | } |
| 102 | |
| 103 | //------------------------------------------------------------------------------ |
| 104 | |
| 105 | } // namespace analyzer |