Gaurav Gandhi | a49a3f7 | 2021-10-26 20:43:25 +0000 | [diff] [blame] | 1 | // Copyright 2021 Google Inc. |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | #include "log_handlers_builder.hpp" |
| 16 | |
| 17 | #include "file_handler.hpp" |
| 18 | #include "fs.hpp" |
| 19 | #include "skip_action.hpp" |
| 20 | |
| 21 | #include <nlohmann/json.hpp> |
| 22 | |
| 23 | #include <algorithm> |
| 24 | #include <cstdio> |
| 25 | #include <exception> |
| 26 | #include <fstream> |
| 27 | #include <memory> |
| 28 | #include <regex> |
| 29 | #include <string> |
| 30 | #include <vector> |
| 31 | |
| 32 | namespace ipmi_flash |
| 33 | { |
| 34 | |
| 35 | std::vector<HandlerConfig<LogBlobHandler::ActionPack>> |
| 36 | LogHandlersBuilder::buildHandlerFromJson(const nlohmann::json& data) |
| 37 | { |
| 38 | std::vector<HandlerConfig<LogBlobHandler::ActionPack>> handlers; |
| 39 | |
| 40 | for (const auto& item : data) |
| 41 | { |
| 42 | try |
| 43 | { |
| 44 | HandlerConfig<LogBlobHandler::ActionPack> output; |
| 45 | |
| 46 | /* at() throws an exception when the key is not present. */ |
| 47 | item.at("blob").get_to(output.blobId); |
| 48 | |
| 49 | /* name must be: /log/ */ |
| 50 | std::regex regexpr("^\\/(?:log)\\/(.+)"); |
| 51 | std::smatch matches; |
| 52 | if (!std::regex_search(output.blobId, matches, regexpr)) |
| 53 | { |
| 54 | continue; |
| 55 | } |
| 56 | /* log must have handler */ |
| 57 | const auto& h = item.at("handler"); |
| 58 | |
| 59 | const std::string& handlerType = h.at("type"); |
| 60 | if (handlerType == "file") |
| 61 | { |
| 62 | const auto& path = h.at("path"); |
| 63 | output.handler = std::make_unique<FileHandler>(path); |
| 64 | } |
| 65 | else |
| 66 | { |
Patrick Williams | 42a44c2 | 2024-08-16 15:21:32 -0400 | [diff] [blame^] | 67 | throw std::runtime_error( |
| 68 | "Invalid handler type: " + handlerType); |
Gaurav Gandhi | a49a3f7 | 2021-10-26 20:43:25 +0000 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | /* actions are required (presently). */ |
| 72 | const auto& a = item.at("actions"); |
| 73 | auto pack = std::make_unique<LogBlobHandler::ActionPack>(); |
| 74 | |
| 75 | /* to make an action optional, assign type "skip" */ |
| 76 | const auto& onOpen = a.at("open"); |
| 77 | const std::string& onOpenType = onOpen.at("type"); |
| 78 | if (onOpenType == "systemd") |
| 79 | { |
| 80 | pack->onOpen = std::move(buildSystemd(onOpen)); |
| 81 | } |
| 82 | else if (onOpenType == "skip") |
| 83 | { |
| 84 | pack->onOpen = SkipAction::CreateSkipAction(); |
| 85 | } |
| 86 | else |
| 87 | { |
Patrick Williams | 42a44c2 | 2024-08-16 15:21:32 -0400 | [diff] [blame^] | 88 | throw std::runtime_error( |
| 89 | "Invalid preparation type: " + onOpenType); |
Gaurav Gandhi | a49a3f7 | 2021-10-26 20:43:25 +0000 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | const auto& onDelete = a.at("delete"); |
Brennan Swanton | 64a23b2 | 2023-02-13 19:45:53 -0800 | [diff] [blame] | 93 | const std::string& onDeleteType = onDelete.at("type"); |
Gaurav Gandhi | 76d37b6 | 2022-10-27 01:14:08 +0000 | [diff] [blame] | 94 | if (onDeleteType == "systemd") |
Gaurav Gandhi | a49a3f7 | 2021-10-26 20:43:25 +0000 | [diff] [blame] | 95 | { |
| 96 | pack->onDelete = std::move(buildSystemd(onDelete)); |
| 97 | } |
Gaurav Gandhi | 76d37b6 | 2022-10-27 01:14:08 +0000 | [diff] [blame] | 98 | else if (onDeleteType == "skip") |
Gaurav Gandhi | a49a3f7 | 2021-10-26 20:43:25 +0000 | [diff] [blame] | 99 | { |
| 100 | pack->onDelete = SkipAction::CreateSkipAction(); |
| 101 | } |
| 102 | else |
| 103 | { |
Patrick Williams | 42a44c2 | 2024-08-16 15:21:32 -0400 | [diff] [blame^] | 104 | throw std::runtime_error( |
| 105 | "Invalid preparation type: " + onDeleteType); |
Gaurav Gandhi | a49a3f7 | 2021-10-26 20:43:25 +0000 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | output.actions = std::move(pack); |
| 109 | handlers.push_back(std::move(output)); |
| 110 | } |
| 111 | catch (const std::exception& e) |
| 112 | { |
| 113 | /* TODO: Once phosphor-logging supports unit-test injection, fix |
| 114 | * this to log. |
| 115 | */ |
| 116 | std::fprintf(stderr, |
| 117 | "Excepted building HandlerConfig from json: %s\n", |
| 118 | e.what()); |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | return handlers; |
| 123 | } |
| 124 | |
| 125 | } // namespace ipmi_flash |