blob: b5b54fb2f8d76744c7ae255420624a12ed7d3128 [file] [log] [blame]
Shawn McCarney6a957f62024-01-10 16:15:19 -06001/**
2 * Copyright © 2024 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 */
16#pragma once
17
Shawn McCarneye02aa3c2025-11-10 12:25:54 -060018#include "chassis.hpp"
Shawn McCarney4840b252025-11-06 16:06:40 -060019#include "power_sequencer_device.hpp"
Shawn McCarney6a957f62024-01-10 16:15:19 -060020#include "rail.hpp"
Shawn McCarney4840b252025-11-06 16:06:40 -060021#include "services.hpp"
Shawn McCarney6a957f62024-01-10 16:15:19 -060022
23#include <nlohmann/json.hpp>
24
Shawn McCarneye140ed92025-11-06 11:20:38 -060025#include <cstdint>
Shawn McCarney6a957f62024-01-10 16:15:19 -060026#include <filesystem>
Shawn McCarneyfb7e0932025-11-10 10:23:05 -060027#include <functional> // for reference_wrapper
Shawn McCarneye140ed92025-11-06 11:20:38 -060028#include <map>
Shawn McCarney906cc3f2024-02-01 13:33:06 -060029#include <memory>
Shawn McCarney6a957f62024-01-10 16:15:19 -060030#include <string>
Shawn McCarneye140ed92025-11-06 11:20:38 -060031#include <tuple>
Shawn McCarney6a957f62024-01-10 16:15:19 -060032#include <vector>
33
Shawn McCarneyfb7e0932025-11-10 10:23:05 -060034using json = nlohmann::json;
35
Shawn McCarney6a957f62024-01-10 16:15:19 -060036namespace phosphor::power::sequencer::config_file_parser
37{
38
39/**
Shawn McCarneye9144ab2024-05-22 12:34:18 -050040 * Standard JSON configuration file directory on the BMC.
41 */
42extern const std::filesystem::path standardConfigFileDirectory;
43
44/**
45 * Finds the JSON configuration file for the current system based on the
46 * specified compatible system types.
47 *
48 * This is required when a single BMC firmware image supports multiple system
49 * types and some system types require different configuration files.
50 *
51 * The compatible system types must be ordered from most to least specific.
52 * Example:
53 * - com.acme.Hardware.Chassis.Model.MegaServer4CPU
54 * - com.acme.Hardware.Chassis.Model.MegaServer
55 * - com.acme.Hardware.Chassis.Model.Server
56 *
57 * Throws an exception if an error occurs.
58 *
59 * @param compatibleSystemTypes compatible system types for the current system
60 * ordered from most to least specific
61 * @param configFileDir directory containing configuration files
62 * @return path to the JSON configuration file, or an empty path if none was
63 * found
64 */
65std::filesystem::path find(
66 const std::vector<std::string>& compatibleSystemTypes,
67 const std::filesystem::path& configFileDir = standardConfigFileDirectory);
68
69/**
Shawn McCarney6a957f62024-01-10 16:15:19 -060070 * Parses the specified JSON configuration file.
71 *
Shawn McCarneyf1d20602025-11-12 17:27:48 -060072 * Returns the corresponding C++ Chassis objects.
Shawn McCarney6a957f62024-01-10 16:15:19 -060073 *
74 * Throws a ConfigFileParserError if an error occurs.
75 *
76 * @param pathName configuration file path name
Shawn McCarneyf1d20602025-11-12 17:27:48 -060077 * @param services system services like hardware presence and the journal
78 * @return vector of Chassis objects
Shawn McCarney6a957f62024-01-10 16:15:19 -060079 */
Shawn McCarneyf1d20602025-11-12 17:27:48 -060080std::vector<std::unique_ptr<Chassis>> parse(
81 const std::filesystem::path& pathName, Services& services);
Shawn McCarney6a957f62024-01-10 16:15:19 -060082
83/*
84 * Internal implementation details for parse()
85 */
86namespace internal
87{
88
Shawn McCarneyfb7e0932025-11-10 10:23:05 -060089using JSONRefWrapper = std::reference_wrapper<const json>;
90
91/**
Shawn McCarneye02aa3c2025-11-10 12:25:54 -060092 * Parses a JSON element containing a chassis object.
93 *
94 * Returns the corresponding C++ Chassis object.
95 *
96 * Throws an exception if parsing fails.
97 *
98 * @param element JSON element
99 * @param chassisTemplates chassis templates map
100 * @param services system services like hardware presence and the journal
101 * @return Chassis object
102 */
103std::unique_ptr<Chassis> parseChassis(
Shawn McCarney1cd07b82025-11-12 13:29:26 -0600104 const json& element,
105 const std::map<std::string, JSONRefWrapper>& chassisTemplates,
Shawn McCarneye02aa3c2025-11-10 12:25:54 -0600106 Services& services);
107
108/**
109 * Parses a JSON element containing an array of chassis objects.
110 *
111 * Returns the corresponding C++ Chassis objects.
112 *
113 * Throws an exception if parsing fails.
114 *
115 * @param element JSON element
116 * @param chassisTemplates chassis templates map
117 * @param services system services like hardware presence and the journal
118 * @return vector of Chassis objects
119 */
120std::vector<std::unique_ptr<Chassis>> parseChassisArray(
Shawn McCarney1cd07b82025-11-12 13:29:26 -0600121 const json& element,
122 const std::map<std::string, JSONRefWrapper>& chassisTemplates,
Shawn McCarneye02aa3c2025-11-10 12:25:54 -0600123 Services& services);
124
125/**
126 * Parses a JSON element containing the properties of a chassis.
127 *
128 * The JSON element may be a chassis object or chassis_template object.
129 *
130 * Returns the corresponding C++ Chassis object.
131 *
132 * Throws an exception if parsing fails.
133 *
134 * @param element JSON element
135 * @param isChassisTemplate specifies whether element is a chassis_template
136 * @param variables variables map used to expand variables in element value
137 * @param services system services like hardware presence and the journal
138 * @return Chassis object
139 */
140std::unique_ptr<Chassis> parseChassisProperties(
141 const json& element, bool isChassisTemplate,
142 const std::map<std::string, std::string>& variables, Services& services);
143
144/**
Shawn McCarneyfb7e0932025-11-10 10:23:05 -0600145 * Parses a JSON element containing a chassis_template object.
146 *
147 * Returns the template ID and a C++ reference_wrapper to the JSON element.
148 *
Shawn McCarney1cd07b82025-11-12 13:29:26 -0600149 * A chassis_template object cannot be fully parsed in isolation. It is a
150 * template that contains variables.
151 *
152 * The chassis_template object is used by one or more chassis objects to avoid
153 * duplicate JSON. The chassis objects define chassis-specific values for the
154 * template variables.
155 *
156 * When the chassis object is parsed, the chassis_template JSON will be
157 * re-parsed, and the template variables will be replaced with the
158 * chassis-specific values.
159 *
Shawn McCarneyfb7e0932025-11-10 10:23:05 -0600160 * Throws an exception if parsing fails.
161 *
162 * @param element JSON element
163 * @return template ID and reference_wrapper to JSON element
164 */
165std::tuple<std::string, JSONRefWrapper> parseChassisTemplate(
166 const json& element);
167
Shawn McCarney6a957f62024-01-10 16:15:19 -0600168/**
Shawn McCarney1cd07b82025-11-12 13:29:26 -0600169 * Parses a JSON element containing an array of chassis_template objects.
170 *
171 * Returns a map of template IDs to chassis_template JSON elements.
172 *
173 * Note that chassis_template objects cannot be fully parsed in isolation. See
174 * parseChassisTemplate() for more information.
175 *
176 * Throws an exception if parsing fails.
177 *
178 * @param element JSON element
179 * @return chassis templates map
180 */
181std::map<std::string, JSONRefWrapper> parseChassisTemplateArray(
182 const json& element);
183
184/**
Shawn McCarney6a957f62024-01-10 16:15:19 -0600185 * Parses a JSON element containing a GPIO.
186 *
187 * Returns the corresponding C++ GPIO object.
188 *
189 * Throws an exception if parsing fails.
190 *
191 * @param element JSON element
Shawn McCarney038f2ba2025-11-06 13:32:16 -0600192 * @param variables variables map used to expand variables in element value
Shawn McCarney6a957f62024-01-10 16:15:19 -0600193 * @return GPIO object
194 */
Shawn McCarneyfb7e0932025-11-10 10:23:05 -0600195GPIO parseGPIO(const json& element,
Shawn McCarney038f2ba2025-11-06 13:32:16 -0600196 const std::map<std::string, std::string>& variables);
Shawn McCarney6a957f62024-01-10 16:15:19 -0600197
198/**
Shawn McCarneye140ed92025-11-06 11:20:38 -0600199 * Parses a JSON element containing an i2c_interface object.
200 *
201 * Returns the corresponding I2C bus and address.
202 *
203 * Throws an exception if parsing fails.
204 *
205 * @param element JSON element
206 * @param variables variables map used to expand variables in element value
207 * @return tuple containing bus and address
208 */
209std::tuple<uint8_t, uint16_t> parseI2CInterface(
Shawn McCarneyfb7e0932025-11-10 10:23:05 -0600210 const json& element, const std::map<std::string, std::string>& variables);
Shawn McCarneye140ed92025-11-06 11:20:38 -0600211
212/**
Shawn McCarney4840b252025-11-06 16:06:40 -0600213 * Parses a JSON element containing a power_sequencer object.
214 *
215 * Returns the corresponding C++ PowerSequencerDevice object.
216 *
217 * Throws an exception if parsing fails.
218 *
219 * @param element JSON element
220 * @param variables variables map used to expand variables in element value
Shawn McCarneyfb7e0932025-11-10 10:23:05 -0600221 * @param services system services like hardware presence and the journal
Shawn McCarney4840b252025-11-06 16:06:40 -0600222 * @return PowerSequencerDevice object
223 */
224std::unique_ptr<PowerSequencerDevice> parsePowerSequencer(
Shawn McCarneyfb7e0932025-11-10 10:23:05 -0600225 const json& element, const std::map<std::string, std::string>& variables,
226 Services& services);
Shawn McCarney4840b252025-11-06 16:06:40 -0600227
228/**
229 * Parses a JSON element containing an array of power_sequencer objects.
230 *
231 * Returns the corresponding C++ PowerSequencerDevice objects.
232 *
233 * Throws an exception if parsing fails.
234 *
235 * @param element JSON element
236 * @param variables variables map used to expand variables in element value
Shawn McCarneyfb7e0932025-11-10 10:23:05 -0600237 * @param services system services like hardware presence and the journal
Shawn McCarney4840b252025-11-06 16:06:40 -0600238 * @return vector of PowerSequencerDevice objects
239 */
240std::vector<std::unique_ptr<PowerSequencerDevice>> parsePowerSequencerArray(
Shawn McCarneyfb7e0932025-11-10 10:23:05 -0600241 const json& element, const std::map<std::string, std::string>& variables,
242 Services& services);
Shawn McCarney4840b252025-11-06 16:06:40 -0600243
244/**
Shawn McCarney6a957f62024-01-10 16:15:19 -0600245 * Parses a JSON element containing a rail.
246 *
247 * Returns the corresponding C++ Rail object.
248 *
249 * Throws an exception if parsing fails.
250 *
251 * @param element JSON element
Shawn McCarney038f2ba2025-11-06 13:32:16 -0600252 * @param variables variables map used to expand variables in element value
Shawn McCarney6a957f62024-01-10 16:15:19 -0600253 * @return Rail object
254 */
Shawn McCarney038f2ba2025-11-06 13:32:16 -0600255std::unique_ptr<Rail> parseRail(
Shawn McCarneyfb7e0932025-11-10 10:23:05 -0600256 const json& element, const std::map<std::string, std::string>& variables);
Shawn McCarney6a957f62024-01-10 16:15:19 -0600257
258/**
259 * Parses a JSON element containing an array of rails.
260 *
261 * Returns the corresponding C++ Rail objects.
262 *
263 * Throws an exception if parsing fails.
264 *
265 * @param element JSON element
Shawn McCarney038f2ba2025-11-06 13:32:16 -0600266 * @param variables variables map used to expand variables in element value
Shawn McCarney6a957f62024-01-10 16:15:19 -0600267 * @return vector of Rail objects
268 */
Patrick Williams92261f82025-02-01 08:22:34 -0500269std::vector<std::unique_ptr<Rail>> parseRailArray(
Shawn McCarneyfb7e0932025-11-10 10:23:05 -0600270 const json& element, const std::map<std::string, std::string>& variables);
Shawn McCarney6a957f62024-01-10 16:15:19 -0600271
272/**
273 * Parses the JSON root element of the entire configuration file.
274 *
Shawn McCarneyf1d20602025-11-12 17:27:48 -0600275 * Returns the corresponding C++ Chassis objects.
Shawn McCarney6a957f62024-01-10 16:15:19 -0600276 *
277 * Throws an exception if parsing fails.
278 *
279 * @param element JSON element
Shawn McCarneyf1d20602025-11-12 17:27:48 -0600280 * @param services system services like hardware presence and the journal
281 * @return vector of Chassis objects
Shawn McCarney6a957f62024-01-10 16:15:19 -0600282 */
Shawn McCarneyf1d20602025-11-12 17:27:48 -0600283std::vector<std::unique_ptr<Chassis>> parseRoot(const json& element,
284 Services& services);
Shawn McCarney6a957f62024-01-10 16:15:19 -0600285
Shawn McCarneye02aa3c2025-11-10 12:25:54 -0600286/**
287 * Parses a JSON element containing an object with variable names and values.
288 *
289 * Returns the corresponding C++ map of variable names and values.
290 *
291 * Throws an exception if parsing fails.
292 *
293 * @param element JSON element
294 * @return map of variable names and values
295 */
296std::map<std::string, std::string> parseVariables(const json& element);
297
Shawn McCarney6a957f62024-01-10 16:15:19 -0600298} // namespace internal
299
300} // namespace phosphor::power::sequencer::config_file_parser