blob: f27fa549e4e13055689a9cc314f17898de6a373e [file] [log] [blame]
Jason Ling85e54f12020-11-05 18:47:21 -08001/*
2 * Copyright 2019 Google Inc.
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 "version_handlers_builder.hpp"
17
18#include "file_handler.hpp"
19#include "fs.hpp"
20#include "skip_action.hpp"
21
22#include <nlohmann/json.hpp>
23
24#include <algorithm>
25#include <cstdio>
26#include <exception>
27#include <fstream>
28#include <memory>
29#include <regex>
30#include <string>
31#include <vector>
32
33namespace ipmi_flash
34{
William A. Kennington IIIabf17352020-12-22 21:07:11 -080035
36std::vector<HandlerConfig<VersionBlobHandler::ActionPack>>
Jason Ling85e54f12020-11-05 18:47:21 -080037 VersionHandlersBuilder::buildHandlerFromJson(const nlohmann::json& data)
38{
William A. Kennington IIIabf17352020-12-22 21:07:11 -080039 std::vector<HandlerConfig<VersionBlobHandler::ActionPack>> handlers;
Jason Ling85e54f12020-11-05 18:47:21 -080040
41 for (const auto& item : data)
42 {
43 try
44 {
William A. Kennington IIIabf17352020-12-22 21:07:11 -080045 HandlerConfig<VersionBlobHandler::ActionPack> output;
Jason Ling85e54f12020-11-05 18:47:21 -080046
47 /* at() throws an exception when the key is not present. */
48 item.at("blob").get_to(output.blobId);
49
50 /* name must be: /flash/... or /version/...*/
51 std::regex regexpr("^\\/(?:flash|version)\\/(.+)");
52 std::smatch matches;
53 if (!std::regex_search(output.blobId, matches, regexpr))
54 {
55 throw std::runtime_error(
56 "Invalid blob name: '" + output.blobId +
57 "' must start with /flash/ or /version/");
58 }
59 output.blobId = "/version/" + matches[1].str();
60 /* version is required. */
61 const auto& v = item.at("version");
62 /* version must have handler */
63 const auto& h = v.at("handler");
64
William A. Kennington III8557a932020-12-22 12:05:10 -080065 const std::string& handlerType = h.at("type");
Jason Ling85e54f12020-11-05 18:47:21 -080066 if (handlerType == "file")
67 {
68 const auto& path = h.at("path");
69 output.handler = std::make_unique<FileHandler>(path);
70 }
71 else
72 {
73 throw std::runtime_error("Invalid handler type: " +
74 handlerType);
75 }
76
77 /* actions are required (presently). */
78 const auto& a = v.at("actions");
William A. Kennington IIIabf17352020-12-22 21:07:11 -080079 auto pack = std::make_unique<VersionBlobHandler::ActionPack>();
Jason Ling85e54f12020-11-05 18:47:21 -080080
81 /* to make an action optional, assign type "skip" */
82 const auto& onOpen = a.at("open");
William A. Kennington III8557a932020-12-22 12:05:10 -080083 const std::string& onOpenType = onOpen.at("type");
Jason Ling85e54f12020-11-05 18:47:21 -080084 if (onOpenType == "systemd")
85 {
86 pack->onOpen = std::move(buildSystemd(onOpen));
87 }
88 else if (onOpenType == "skip")
89 {
90 pack->onOpen = SkipAction::CreateSkipAction();
91 }
92 else
93 {
94 throw std::runtime_error("Invalid preparation type: " +
95 onOpenType);
96 }
97
98 output.actions = std::move(pack);
99 handlers.push_back(std::move(output));
100 }
101 catch (const std::exception& e)
102 {
103 /* TODO: Once phosphor-logging supports unit-test injection, fix
104 * this to log.
105 */
106 std::fprintf(stderr,
107 "Excepted building HandlerConfig from json: %s\n",
108 e.what());
109 }
110 }
111
112 return handlers;
113}
William A. Kennington IIIabf17352020-12-22 21:07:11 -0800114
Jason Ling85e54f12020-11-05 18:47:21 -0800115} // namespace ipmi_flash