blob: fe60cce257c59968452d706bbd6492b885661105 [file] [log] [blame]
Gaurav Gandhia49a3f72021-10-26 20:43:25 +00001// 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
32namespace ipmi_flash
33{
34
35std::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 Williams42a44c22024-08-16 15:21:32 -040067 throw std::runtime_error(
68 "Invalid handler type: " + handlerType);
Gaurav Gandhia49a3f72021-10-26 20:43:25 +000069 }
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 Williams42a44c22024-08-16 15:21:32 -040088 throw std::runtime_error(
89 "Invalid preparation type: " + onOpenType);
Gaurav Gandhia49a3f72021-10-26 20:43:25 +000090 }
91
92 const auto& onDelete = a.at("delete");
Brennan Swanton64a23b22023-02-13 19:45:53 -080093 const std::string& onDeleteType = onDelete.at("type");
Gaurav Gandhi76d37b62022-10-27 01:14:08 +000094 if (onDeleteType == "systemd")
Gaurav Gandhia49a3f72021-10-26 20:43:25 +000095 {
96 pack->onDelete = std::move(buildSystemd(onDelete));
97 }
Gaurav Gandhi76d37b62022-10-27 01:14:08 +000098 else if (onDeleteType == "skip")
Gaurav Gandhia49a3f72021-10-26 20:43:25 +000099 {
100 pack->onDelete = SkipAction::CreateSkipAction();
101 }
102 else
103 {
Patrick Williams42a44c22024-08-16 15:21:32 -0400104 throw std::runtime_error(
105 "Invalid preparation type: " + onDeleteType);
Gaurav Gandhia49a3f72021-10-26 20:43:25 +0000106 }
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