| Shawn McCarney | 6a957f6 | 2024-01-10 16:15:19 -0600 | [diff] [blame] | 1 | /** |
| 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 | |
| 17 | #include "config_file_parser.hpp" |
| 18 | |
| 19 | #include "config_file_parser_error.hpp" |
| Shawn McCarney | 38f8500 | 2025-10-31 17:59:36 -0500 | [diff] [blame] | 20 | #include "json_parser_utils.hpp" |
| Shawn McCarney | 4840b25 | 2025-11-06 16:06:40 -0600 | [diff] [blame] | 21 | #include "ucd90160_device.hpp" |
| 22 | #include "ucd90320_device.hpp" |
| Shawn McCarney | 6a957f6 | 2024-01-10 16:15:19 -0600 | [diff] [blame] | 23 | |
| Shawn McCarney | 38f8500 | 2025-10-31 17:59:36 -0500 | [diff] [blame] | 24 | #include <cstdint> |
| Shawn McCarney | 6a957f6 | 2024-01-10 16:15:19 -0600 | [diff] [blame] | 25 | #include <exception> |
| 26 | #include <fstream> |
| 27 | #include <optional> |
| Shawn McCarney | 38f8500 | 2025-10-31 17:59:36 -0500 | [diff] [blame] | 28 | #include <stdexcept> |
| Shawn McCarney | 6a957f6 | 2024-01-10 16:15:19 -0600 | [diff] [blame] | 29 | |
| Shawn McCarney | 38f8500 | 2025-10-31 17:59:36 -0500 | [diff] [blame] | 30 | using namespace phosphor::power::json_parser_utils; |
| 31 | using ConfigFileParserError = phosphor::power::util::ConfigFileParserError; |
| Shawn McCarney | e9144ab | 2024-05-22 12:34:18 -0500 | [diff] [blame] | 32 | namespace fs = std::filesystem; |
| Shawn McCarney | 6a957f6 | 2024-01-10 16:15:19 -0600 | [diff] [blame] | 33 | |
| 34 | namespace phosphor::power::sequencer::config_file_parser |
| 35 | { |
| 36 | |
| Shawn McCarney | e9144ab | 2024-05-22 12:34:18 -0500 | [diff] [blame] | 37 | const std::filesystem::path standardConfigFileDirectory{ |
| 38 | "/usr/share/phosphor-power-sequencer"}; |
| 39 | |
| Patrick Williams | 92261f8 | 2025-02-01 08:22:34 -0500 | [diff] [blame] | 40 | std::filesystem::path find( |
| 41 | const std::vector<std::string>& compatibleSystemTypes, |
| 42 | const std::filesystem::path& configFileDir) |
| Shawn McCarney | e9144ab | 2024-05-22 12:34:18 -0500 | [diff] [blame] | 43 | { |
| 44 | fs::path pathName, possiblePath; |
| 45 | std::string fileName; |
| 46 | |
| 47 | for (const std::string& systemType : compatibleSystemTypes) |
| 48 | { |
| 49 | // Look for file name that is entire system type + ".json" |
| 50 | // Example: com.acme.Hardware.Chassis.Model.MegaServer.json |
| 51 | fileName = systemType + ".json"; |
| 52 | possiblePath = configFileDir / fileName; |
| 53 | if (fs::is_regular_file(possiblePath)) |
| 54 | { |
| 55 | pathName = possiblePath; |
| 56 | break; |
| 57 | } |
| 58 | |
| 59 | // Look for file name that is last node of system type + ".json" |
| 60 | // Example: MegaServer.json |
| 61 | std::string::size_type pos = systemType.rfind('.'); |
| 62 | if ((pos != std::string::npos) && ((systemType.size() - pos) > 1)) |
| 63 | { |
| 64 | fileName = systemType.substr(pos + 1) + ".json"; |
| 65 | possiblePath = configFileDir / fileName; |
| 66 | if (fs::is_regular_file(possiblePath)) |
| 67 | { |
| 68 | pathName = possiblePath; |
| 69 | break; |
| 70 | } |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | return pathName; |
| 75 | } |
| 76 | |
| Shawn McCarney | 6a957f6 | 2024-01-10 16:15:19 -0600 | [diff] [blame] | 77 | std::vector<std::unique_ptr<Rail>> parse(const std::filesystem::path& pathName) |
| 78 | { |
| 79 | try |
| 80 | { |
| 81 | // Use standard JSON parser to create tree of JSON elements |
| 82 | std::ifstream file{pathName}; |
| 83 | json rootElement = json::parse(file); |
| 84 | |
| 85 | // Parse tree of JSON elements and return corresponding C++ objects |
| 86 | return internal::parseRoot(rootElement); |
| 87 | } |
| 88 | catch (const std::exception& e) |
| 89 | { |
| 90 | throw ConfigFileParserError{pathName, e.what()}; |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | namespace internal |
| 95 | { |
| 96 | |
| Shawn McCarney | e02aa3c | 2025-11-10 12:25:54 -0600 | [diff] [blame^] | 97 | std::unique_ptr<Chassis> parseChassis( |
| 98 | const json& element, std::map<std::string, JSONRefWrapper> chassisTemplates, |
| 99 | Services& services) |
| 100 | { |
| 101 | verifyIsObject(element); |
| 102 | |
| 103 | // If chassis object is not using a template, parse properties normally |
| 104 | if (!element.contains("template_id")) |
| 105 | { |
| 106 | bool isChassisTemplate{false}; |
| 107 | return parseChassisProperties(element, isChassisTemplate, NO_VARIABLES, |
| 108 | services); |
| 109 | } |
| 110 | |
| 111 | // Parse chassis object that is using a template |
| 112 | unsigned int propertyCount{0}; |
| 113 | |
| 114 | // Optional comments property; value not stored |
| 115 | if (element.contains("comments")) |
| 116 | { |
| 117 | ++propertyCount; |
| 118 | } |
| 119 | |
| 120 | // Required template_id property |
| 121 | const json& templateIDElement = getRequiredProperty(element, "template_id"); |
| 122 | std::string templateID = parseString(templateIDElement); |
| 123 | ++propertyCount; |
| 124 | |
| 125 | // Required template_variable_values property |
| 126 | const json& variablesElement = |
| 127 | getRequiredProperty(element, "template_variable_values"); |
| 128 | std::map<std::string, std::string> variables = |
| 129 | parseVariables(variablesElement); |
| 130 | ++propertyCount; |
| 131 | |
| 132 | // Verify no invalid properties exist |
| 133 | verifyPropertyCount(element, propertyCount); |
| 134 | |
| 135 | // Get reference to chassis template JSON |
| 136 | auto it = chassisTemplates.find(templateID); |
| 137 | if (it == chassisTemplates.end()) |
| 138 | { |
| 139 | throw std::invalid_argument{ |
| 140 | "Invalid chassis template id: " + templateID}; |
| 141 | } |
| 142 | const json& templateElement = it->second.get(); |
| 143 | |
| 144 | // Parse properties in template using variable values for this chassis |
| 145 | bool isChassisTemplate{true}; |
| 146 | return parseChassisProperties(templateElement, isChassisTemplate, variables, |
| 147 | services); |
| 148 | } |
| 149 | |
| 150 | std::vector<std::unique_ptr<Chassis>> parseChassisArray( |
| 151 | const json& element, std::map<std::string, JSONRefWrapper> chassisTemplates, |
| 152 | Services& services) |
| 153 | { |
| 154 | verifyIsArray(element); |
| 155 | std::vector<std::unique_ptr<Chassis>> chassis; |
| 156 | for (auto& chassisElement : element) |
| 157 | { |
| 158 | chassis.emplace_back( |
| 159 | parseChassis(chassisElement, chassisTemplates, services)); |
| 160 | } |
| 161 | return chassis; |
| 162 | } |
| 163 | |
| 164 | std::unique_ptr<Chassis> parseChassisProperties( |
| 165 | const json& element, bool isChassisTemplate, |
| 166 | const std::map<std::string, std::string>& variables, Services& services) |
| 167 | |
| 168 | { |
| 169 | verifyIsObject(element); |
| 170 | unsigned int propertyCount{0}; |
| 171 | |
| 172 | // Optional comments property; value not stored |
| 173 | if (element.contains("comments")) |
| 174 | { |
| 175 | ++propertyCount; |
| 176 | } |
| 177 | |
| 178 | // Required id property if this is a chassis template |
| 179 | // Don't parse again; this was already parsed by parseChassisTemplate() |
| 180 | if (isChassisTemplate) |
| 181 | { |
| 182 | getRequiredProperty(element, "id"); |
| 183 | ++propertyCount; |
| 184 | } |
| 185 | |
| 186 | // Required number property |
| 187 | const json& numberElement = getRequiredProperty(element, "number"); |
| 188 | unsigned int number = parseUnsignedInteger(numberElement, variables); |
| 189 | if (number < 1) |
| 190 | { |
| 191 | throw std::invalid_argument{"Invalid chassis number: Must be > 0"}; |
| 192 | } |
| 193 | ++propertyCount; |
| 194 | |
| 195 | // Required inventory_path property |
| 196 | const json& inventoryPathElement = |
| 197 | getRequiredProperty(element, "inventory_path"); |
| 198 | std::string inventoryPath = |
| 199 | parseString(inventoryPathElement, false, variables); |
| 200 | ++propertyCount; |
| 201 | |
| 202 | // Required power_sequencers property |
| 203 | const json& powerSequencersElement = |
| 204 | getRequiredProperty(element, "power_sequencers"); |
| 205 | std::vector<std::unique_ptr<PowerSequencerDevice>> powerSequencers = |
| 206 | parsePowerSequencerArray(powerSequencersElement, variables, services); |
| 207 | ++propertyCount; |
| 208 | |
| 209 | // Verify no invalid properties exist |
| 210 | verifyPropertyCount(element, propertyCount); |
| 211 | |
| 212 | return std::make_unique<Chassis>(number, inventoryPath, |
| 213 | std::move(powerSequencers)); |
| 214 | } |
| 215 | |
| Shawn McCarney | fb7e093 | 2025-11-10 10:23:05 -0600 | [diff] [blame] | 216 | std::tuple<std::string, JSONRefWrapper> parseChassisTemplate( |
| 217 | const json& element) |
| 218 | { |
| 219 | verifyIsObject(element); |
| 220 | unsigned int propertyCount{0}; |
| 221 | |
| 222 | // Optional comments property; value not stored |
| 223 | if (element.contains("comments")) |
| 224 | { |
| 225 | ++propertyCount; |
| 226 | } |
| 227 | |
| 228 | // Required id property |
| 229 | const json& idElement = getRequiredProperty(element, "id"); |
| 230 | std::string id = parseString(idElement); |
| 231 | ++propertyCount; |
| 232 | |
| 233 | // Required number property |
| 234 | // Just verify it exists; cannot be parsed without variable values |
| 235 | getRequiredProperty(element, "number"); |
| 236 | ++propertyCount; |
| 237 | |
| 238 | // Required inventory_path property |
| 239 | // Just verify it exists; cannot be parsed without variable values |
| 240 | getRequiredProperty(element, "inventory_path"); |
| 241 | ++propertyCount; |
| 242 | |
| 243 | // Required power_sequencers property |
| 244 | // Just verify it exists; cannot be parsed without variable values |
| 245 | getRequiredProperty(element, "power_sequencers"); |
| 246 | ++propertyCount; |
| 247 | |
| 248 | // Verify no invalid properties exist |
| 249 | verifyPropertyCount(element, propertyCount); |
| 250 | |
| 251 | return {id, JSONRefWrapper{element}}; |
| 252 | } |
| 253 | |
| Shawn McCarney | 038f2ba | 2025-11-06 13:32:16 -0600 | [diff] [blame] | 254 | GPIO parseGPIO(const json& element, |
| 255 | const std::map<std::string, std::string>& variables) |
| Shawn McCarney | 6a957f6 | 2024-01-10 16:15:19 -0600 | [diff] [blame] | 256 | { |
| 257 | verifyIsObject(element); |
| 258 | unsigned int propertyCount{0}; |
| 259 | |
| 260 | // Required line property |
| 261 | const json& lineElement = getRequiredProperty(element, "line"); |
| Shawn McCarney | 038f2ba | 2025-11-06 13:32:16 -0600 | [diff] [blame] | 262 | unsigned int line = parseUnsignedInteger(lineElement, variables); |
| Shawn McCarney | 6a957f6 | 2024-01-10 16:15:19 -0600 | [diff] [blame] | 263 | ++propertyCount; |
| 264 | |
| 265 | // Optional active_low property |
| 266 | bool activeLow{false}; |
| 267 | auto activeLowIt = element.find("active_low"); |
| 268 | if (activeLowIt != element.end()) |
| 269 | { |
| Shawn McCarney | 038f2ba | 2025-11-06 13:32:16 -0600 | [diff] [blame] | 270 | activeLow = parseBoolean(*activeLowIt, variables); |
| Shawn McCarney | 6a957f6 | 2024-01-10 16:15:19 -0600 | [diff] [blame] | 271 | ++propertyCount; |
| 272 | } |
| 273 | |
| 274 | // Verify no invalid properties exist |
| 275 | verifyPropertyCount(element, propertyCount); |
| 276 | |
| 277 | return GPIO(line, activeLow); |
| 278 | } |
| 279 | |
| Shawn McCarney | e140ed9 | 2025-11-06 11:20:38 -0600 | [diff] [blame] | 280 | std::tuple<uint8_t, uint16_t> parseI2CInterface( |
| 281 | const nlohmann::json& element, |
| 282 | const std::map<std::string, std::string>& variables) |
| 283 | { |
| 284 | verifyIsObject(element); |
| 285 | unsigned int propertyCount{0}; |
| 286 | |
| 287 | // Required bus property |
| 288 | const json& busElement = getRequiredProperty(element, "bus"); |
| 289 | uint8_t bus = parseUint8(busElement, variables); |
| 290 | ++propertyCount; |
| 291 | |
| 292 | // Required address property |
| 293 | const json& addressElement = getRequiredProperty(element, "address"); |
| 294 | uint16_t address = parseHexByte(addressElement, variables); |
| 295 | ++propertyCount; |
| 296 | |
| 297 | // Verify no invalid properties exist |
| 298 | verifyPropertyCount(element, propertyCount); |
| 299 | |
| 300 | return {bus, address}; |
| 301 | } |
| 302 | |
| Shawn McCarney | 4840b25 | 2025-11-06 16:06:40 -0600 | [diff] [blame] | 303 | std::unique_ptr<PowerSequencerDevice> parsePowerSequencer( |
| 304 | const nlohmann::json& element, |
| 305 | const std::map<std::string, std::string>& variables, Services& services) |
| 306 | { |
| 307 | verifyIsObject(element); |
| 308 | unsigned int propertyCount{0}; |
| 309 | |
| 310 | // Optional comments property; value not stored |
| 311 | if (element.contains("comments")) |
| 312 | { |
| 313 | ++propertyCount; |
| 314 | } |
| 315 | |
| 316 | // Required type property |
| 317 | const json& typeElement = getRequiredProperty(element, "type"); |
| 318 | std::string type = parseString(typeElement, false, variables); |
| 319 | ++propertyCount; |
| 320 | |
| 321 | // Required i2c_interface property |
| 322 | const json& i2cInterfaceElement = |
| 323 | getRequiredProperty(element, "i2c_interface"); |
| 324 | auto [bus, address] = parseI2CInterface(i2cInterfaceElement, variables); |
| 325 | ++propertyCount; |
| 326 | |
| 327 | // Required power_control_gpio_name property |
| 328 | const json& powerControlGPIONameElement = |
| 329 | getRequiredProperty(element, "power_control_gpio_name"); |
| 330 | std::string powerControlGPIOName = |
| 331 | parseString(powerControlGPIONameElement, false, variables); |
| 332 | ++propertyCount; |
| 333 | |
| 334 | // Required power_good_gpio_name property |
| 335 | const json& powerGoodGPIONameElement = |
| 336 | getRequiredProperty(element, "power_good_gpio_name"); |
| 337 | std::string powerGoodGPIOName = |
| 338 | parseString(powerGoodGPIONameElement, false, variables); |
| 339 | ++propertyCount; |
| 340 | |
| 341 | // Required rails property |
| 342 | const json& railsElement = getRequiredProperty(element, "rails"); |
| 343 | std::vector<std::unique_ptr<Rail>> rails = |
| 344 | parseRailArray(railsElement, variables); |
| 345 | ++propertyCount; |
| 346 | |
| 347 | // Verify no invalid properties exist |
| 348 | verifyPropertyCount(element, propertyCount); |
| 349 | |
| 350 | if (type == UCD90160Device::deviceName) |
| 351 | { |
| 352 | return std::make_unique<UCD90160Device>( |
| 353 | bus, address, powerControlGPIOName, powerGoodGPIOName, |
| 354 | std::move(rails), services); |
| 355 | } |
| 356 | else if (type == UCD90320Device::deviceName) |
| 357 | { |
| 358 | return std::make_unique<UCD90320Device>( |
| 359 | bus, address, powerControlGPIOName, powerGoodGPIOName, |
| 360 | std::move(rails), services); |
| 361 | } |
| 362 | throw std::invalid_argument{"Invalid power sequencer type: " + type}; |
| 363 | } |
| 364 | |
| 365 | std::vector<std::unique_ptr<PowerSequencerDevice>> parsePowerSequencerArray( |
| 366 | const nlohmann::json& element, |
| 367 | const std::map<std::string, std::string>& variables, Services& services) |
| 368 | { |
| 369 | verifyIsArray(element); |
| 370 | std::vector<std::unique_ptr<PowerSequencerDevice>> powerSequencers; |
| 371 | for (auto& powerSequencerElement : element) |
| 372 | { |
| 373 | powerSequencers.emplace_back( |
| 374 | parsePowerSequencer(powerSequencerElement, variables, services)); |
| 375 | } |
| 376 | return powerSequencers; |
| 377 | } |
| 378 | |
| Shawn McCarney | 038f2ba | 2025-11-06 13:32:16 -0600 | [diff] [blame] | 379 | std::unique_ptr<Rail> parseRail( |
| 380 | const json& element, const std::map<std::string, std::string>& variables) |
| Shawn McCarney | 6a957f6 | 2024-01-10 16:15:19 -0600 | [diff] [blame] | 381 | { |
| 382 | verifyIsObject(element); |
| 383 | unsigned int propertyCount{0}; |
| 384 | |
| 385 | // Required name property |
| 386 | const json& nameElement = getRequiredProperty(element, "name"); |
| Shawn McCarney | 038f2ba | 2025-11-06 13:32:16 -0600 | [diff] [blame] | 387 | std::string name = parseString(nameElement, false, variables); |
| Shawn McCarney | 6a957f6 | 2024-01-10 16:15:19 -0600 | [diff] [blame] | 388 | ++propertyCount; |
| 389 | |
| 390 | // Optional presence property |
| 391 | std::optional<std::string> presence{}; |
| 392 | auto presenceIt = element.find("presence"); |
| 393 | if (presenceIt != element.end()) |
| 394 | { |
| Shawn McCarney | 038f2ba | 2025-11-06 13:32:16 -0600 | [diff] [blame] | 395 | presence = parseString(*presenceIt, false, variables); |
| Shawn McCarney | 6a957f6 | 2024-01-10 16:15:19 -0600 | [diff] [blame] | 396 | ++propertyCount; |
| 397 | } |
| 398 | |
| 399 | // Optional page property |
| 400 | std::optional<uint8_t> page{}; |
| 401 | auto pageIt = element.find("page"); |
| 402 | if (pageIt != element.end()) |
| 403 | { |
| Shawn McCarney | 038f2ba | 2025-11-06 13:32:16 -0600 | [diff] [blame] | 404 | page = parseUint8(*pageIt, variables); |
| Shawn McCarney | 6a957f6 | 2024-01-10 16:15:19 -0600 | [diff] [blame] | 405 | ++propertyCount; |
| 406 | } |
| 407 | |
| Shawn McCarney | 16e493a | 2024-01-29 14:20:32 -0600 | [diff] [blame] | 408 | // Optional is_power_supply_rail property |
| 409 | bool isPowerSupplyRail{false}; |
| 410 | auto isPowerSupplyRailIt = element.find("is_power_supply_rail"); |
| 411 | if (isPowerSupplyRailIt != element.end()) |
| 412 | { |
| Shawn McCarney | 038f2ba | 2025-11-06 13:32:16 -0600 | [diff] [blame] | 413 | isPowerSupplyRail = parseBoolean(*isPowerSupplyRailIt, variables); |
| Shawn McCarney | 16e493a | 2024-01-29 14:20:32 -0600 | [diff] [blame] | 414 | ++propertyCount; |
| 415 | } |
| 416 | |
| Shawn McCarney | 6a957f6 | 2024-01-10 16:15:19 -0600 | [diff] [blame] | 417 | // Optional check_status_vout property |
| 418 | bool checkStatusVout{false}; |
| 419 | auto checkStatusVoutIt = element.find("check_status_vout"); |
| 420 | if (checkStatusVoutIt != element.end()) |
| 421 | { |
| Shawn McCarney | 038f2ba | 2025-11-06 13:32:16 -0600 | [diff] [blame] | 422 | checkStatusVout = parseBoolean(*checkStatusVoutIt, variables); |
| Shawn McCarney | 6a957f6 | 2024-01-10 16:15:19 -0600 | [diff] [blame] | 423 | ++propertyCount; |
| 424 | } |
| 425 | |
| Shawn McCarney | 9ec0d43 | 2024-02-09 18:26:00 -0600 | [diff] [blame] | 426 | // Optional compare_voltage_to_limit property |
| 427 | bool compareVoltageToLimit{false}; |
| 428 | auto compareVoltageToLimitIt = element.find("compare_voltage_to_limit"); |
| 429 | if (compareVoltageToLimitIt != element.end()) |
| Shawn McCarney | 6a957f6 | 2024-01-10 16:15:19 -0600 | [diff] [blame] | 430 | { |
| Shawn McCarney | 038f2ba | 2025-11-06 13:32:16 -0600 | [diff] [blame] | 431 | compareVoltageToLimit = |
| 432 | parseBoolean(*compareVoltageToLimitIt, variables); |
| Shawn McCarney | 6a957f6 | 2024-01-10 16:15:19 -0600 | [diff] [blame] | 433 | ++propertyCount; |
| 434 | } |
| 435 | |
| 436 | // Optional gpio property |
| 437 | std::optional<GPIO> gpio{}; |
| 438 | auto gpioIt = element.find("gpio"); |
| 439 | if (gpioIt != element.end()) |
| 440 | { |
| Shawn McCarney | 038f2ba | 2025-11-06 13:32:16 -0600 | [diff] [blame] | 441 | gpio = parseGPIO(*gpioIt, variables); |
| Shawn McCarney | 6a957f6 | 2024-01-10 16:15:19 -0600 | [diff] [blame] | 442 | ++propertyCount; |
| 443 | } |
| 444 | |
| Shawn McCarney | 9ec0d43 | 2024-02-09 18:26:00 -0600 | [diff] [blame] | 445 | // If check_status_vout or compare_voltage_to_limit property is true, the |
| 446 | // page property is required; verify page was specified |
| 447 | if ((checkStatusVout || compareVoltageToLimit) && !page.has_value()) |
| Shawn McCarney | 6a957f6 | 2024-01-10 16:15:19 -0600 | [diff] [blame] | 448 | { |
| 449 | throw std::invalid_argument{"Required property missing: page"}; |
| 450 | } |
| 451 | |
| 452 | // Verify no invalid properties exist |
| 453 | verifyPropertyCount(element, propertyCount); |
| 454 | |
| Shawn McCarney | 16e493a | 2024-01-29 14:20:32 -0600 | [diff] [blame] | 455 | return std::make_unique<Rail>(name, presence, page, isPowerSupplyRail, |
| Shawn McCarney | 9ec0d43 | 2024-02-09 18:26:00 -0600 | [diff] [blame] | 456 | checkStatusVout, compareVoltageToLimit, gpio); |
| Shawn McCarney | 6a957f6 | 2024-01-10 16:15:19 -0600 | [diff] [blame] | 457 | } |
| 458 | |
| Shawn McCarney | 038f2ba | 2025-11-06 13:32:16 -0600 | [diff] [blame] | 459 | std::vector<std::unique_ptr<Rail>> parseRailArray( |
| 460 | const json& element, const std::map<std::string, std::string>& variables) |
| Shawn McCarney | 6a957f6 | 2024-01-10 16:15:19 -0600 | [diff] [blame] | 461 | { |
| 462 | verifyIsArray(element); |
| 463 | std::vector<std::unique_ptr<Rail>> rails; |
| 464 | for (auto& railElement : element) |
| 465 | { |
| Shawn McCarney | 038f2ba | 2025-11-06 13:32:16 -0600 | [diff] [blame] | 466 | rails.emplace_back(parseRail(railElement, variables)); |
| Shawn McCarney | 6a957f6 | 2024-01-10 16:15:19 -0600 | [diff] [blame] | 467 | } |
| 468 | return rails; |
| 469 | } |
| 470 | |
| 471 | std::vector<std::unique_ptr<Rail>> parseRoot(const json& element) |
| 472 | { |
| Shawn McCarney | 038f2ba | 2025-11-06 13:32:16 -0600 | [diff] [blame] | 473 | std::map<std::string, std::string> variables{}; |
| 474 | |
| Shawn McCarney | 6a957f6 | 2024-01-10 16:15:19 -0600 | [diff] [blame] | 475 | verifyIsObject(element); |
| 476 | unsigned int propertyCount{0}; |
| 477 | |
| 478 | // Required rails property |
| 479 | const json& railsElement = getRequiredProperty(element, "rails"); |
| Shawn McCarney | 038f2ba | 2025-11-06 13:32:16 -0600 | [diff] [blame] | 480 | std::vector<std::unique_ptr<Rail>> rails = |
| 481 | parseRailArray(railsElement, variables); |
| Shawn McCarney | 6a957f6 | 2024-01-10 16:15:19 -0600 | [diff] [blame] | 482 | ++propertyCount; |
| 483 | |
| 484 | // Verify no invalid properties exist |
| 485 | verifyPropertyCount(element, propertyCount); |
| 486 | |
| 487 | return rails; |
| 488 | } |
| 489 | |
| Shawn McCarney | e02aa3c | 2025-11-10 12:25:54 -0600 | [diff] [blame^] | 490 | std::map<std::string, std::string> parseVariables(const json& element) |
| 491 | { |
| 492 | verifyIsObject(element); |
| 493 | |
| 494 | std::map<std::string, std::string> variables; |
| 495 | std::string name, value; |
| 496 | for (const auto& [nameElement, valueElement] : element.items()) |
| 497 | { |
| 498 | name = parseString(nameElement); |
| 499 | value = parseString(valueElement); |
| 500 | variables.emplace(name, value); |
| 501 | } |
| 502 | return variables; |
| 503 | } |
| 504 | |
| Shawn McCarney | 6a957f6 | 2024-01-10 16:15:19 -0600 | [diff] [blame] | 505 | } // namespace internal |
| 506 | |
| 507 | } // namespace phosphor::power::sequencer::config_file_parser |