1 /** 2 * Copyright © 2020 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 18 #include "action.hpp" 19 #include "and_action.hpp" 20 #include "chassis.hpp" 21 #include "compare_presence_action.hpp" 22 #include "compare_vpd_action.hpp" 23 #include "configuration.hpp" 24 #include "device.hpp" 25 #include "i2c_capture_bytes_action.hpp" 26 #include "i2c_compare_bit_action.hpp" 27 #include "i2c_compare_byte_action.hpp" 28 #include "i2c_compare_bytes_action.hpp" 29 #include "i2c_interface.hpp" 30 #include "i2c_write_bit_action.hpp" 31 #include "i2c_write_byte_action.hpp" 32 #include "i2c_write_bytes_action.hpp" 33 #include "if_action.hpp" 34 #include "log_phase_fault_action.hpp" 35 #include "not_action.hpp" 36 #include "or_action.hpp" 37 #include "phase_fault.hpp" 38 #include "phase_fault_detection.hpp" 39 #include "pmbus_read_sensor_action.hpp" 40 #include "pmbus_write_vout_command_action.hpp" 41 #include "presence_detection.hpp" 42 #include "rail.hpp" 43 #include "rule.hpp" 44 #include "run_rule_action.hpp" 45 #include "sensor_monitoring.hpp" 46 #include "sensors.hpp" 47 #include "set_device_action.hpp" 48 49 #include <nlohmann/json.hpp> 50 51 #include <filesystem> 52 #include <memory> 53 #include <string> 54 #include <tuple> 55 #include <vector> 56 57 namespace phosphor::power::regulators::config_file_parser 58 { 59 60 /** 61 * Parses the specified JSON configuration file. 62 * 63 * Returns the corresponding C++ Rule and Chassis objects. 64 * 65 * Throws a ConfigFileParserError if an error occurs. 66 * 67 * @param pathName configuration file path name 68 * @return tuple containing vectors of Rule and Chassis objects 69 */ 70 std::tuple<std::vector<std::unique_ptr<Rule>>, 71 std::vector<std::unique_ptr<Chassis>>> 72 parse(const std::filesystem::path& pathName); 73 74 /* 75 * Internal implementation details for parse() 76 */ 77 namespace internal 78 { 79 80 /** 81 * Parses a JSON element containing an action. 82 * 83 * Returns the corresponding C++ Action object. 84 * 85 * Throws an exception if parsing fails. 86 * 87 * @param element JSON element 88 * @return Action object 89 */ 90 std::unique_ptr<Action> parseAction(const nlohmann::json& element); 91 92 /** 93 * Parses a JSON element containing an array of actions. 94 * 95 * Returns the corresponding C++ Action objects. 96 * 97 * Throws an exception if parsing fails. 98 * 99 * @param element JSON element 100 * @return vector of Action objects 101 */ 102 std::vector<std::unique_ptr<Action>> parseActionArray( 103 const nlohmann::json& element); 104 105 /** 106 * Parses a JSON element containing an and action. 107 * 108 * Returns the corresponding C++ AndAction object. 109 * 110 * Throws an exception if parsing fails. 111 * 112 * @param element JSON element 113 * @return AndAction object 114 */ 115 std::unique_ptr<AndAction> parseAnd(const nlohmann::json& element); 116 117 /** 118 * Parses a JSON element containing a chassis. 119 * 120 * Returns the corresponding C++ Chassis object. 121 * 122 * Throws an exception if parsing fails. 123 * 124 * @param element JSON element 125 * @return Chassis object 126 */ 127 std::unique_ptr<Chassis> parseChassis(const nlohmann::json& element); 128 129 /** 130 * Parses a JSON element containing an array of chassis. 131 * 132 * Returns the corresponding C++ Chassis objects. 133 * 134 * Throws an exception if parsing fails. 135 * 136 * @param element JSON element 137 * @return vector of Chassis objects 138 */ 139 std::vector<std::unique_ptr<Chassis>> parseChassisArray( 140 const nlohmann::json& element); 141 142 /** 143 * Parses a JSON element containing a compare_presence action. 144 * 145 * Returns the corresponding C++ ComparePresenceAction object. 146 * 147 * Throws an exception if parsing fails. 148 * 149 * @param element JSON element 150 * @return ComparePresenceAction object 151 */ 152 std::unique_ptr<ComparePresenceAction> parseComparePresence( 153 const nlohmann::json& element); 154 155 /** 156 * Parses a JSON element containing a compare_vpd action. 157 * 158 * Returns the corresponding C++ CompareVPDAction object. 159 * 160 * Throws an exception if parsing fails. 161 * 162 * @param element JSON element 163 * @return CompareVPDAction object 164 */ 165 std::unique_ptr<CompareVPDAction> parseCompareVPD( 166 const nlohmann::json& element); 167 168 /** 169 * Parses a JSON element containing a configuration object. 170 * 171 * Returns the corresponding C++ Configuration object. 172 * 173 * Throws an exception if parsing fails. 174 * 175 * @param element JSON element 176 * @return Configuration object 177 */ 178 std::unique_ptr<Configuration> parseConfiguration( 179 const nlohmann::json& element); 180 181 /** 182 * Parses a JSON element containing a device. 183 * 184 * Returns the corresponding C++ Device object. 185 * 186 * Throws an exception if parsing fails. 187 * 188 * @param element JSON element 189 * @return Device object 190 */ 191 std::unique_ptr<Device> parseDevice(const nlohmann::json& element); 192 193 /** 194 * Parses a JSON element containing an array of devices. 195 * 196 * Returns the corresponding C++ Device objects. 197 * 198 * Throws an exception if parsing fails. 199 * 200 * @param element JSON element 201 * @return vector of Device objects 202 */ 203 std::vector<std::unique_ptr<Device>> parseDeviceArray( 204 const nlohmann::json& element); 205 206 /** 207 * Parses a JSON element containing an i2c_capture_bytes action. 208 * 209 * Returns the corresponding C++ I2CCaptureBytesAction object. 210 * 211 * Throws an exception if parsing fails. 212 * 213 * @param element JSON element 214 * @return I2CCaptureBytesAction object 215 */ 216 std::unique_ptr<I2CCaptureBytesAction> parseI2CCaptureBytes( 217 const nlohmann::json& element); 218 219 /** 220 * Parses a JSON element containing an i2c_compare_bit action. 221 * 222 * Returns the corresponding C++ I2CCompareBitAction object. 223 * 224 * Throws an exception if parsing fails. 225 * 226 * @param element JSON element 227 * @return I2CCompareBitAction object 228 */ 229 std::unique_ptr<I2CCompareBitAction> parseI2CCompareBit( 230 const nlohmann::json& element); 231 232 /** 233 * Parses a JSON element containing an i2c_compare_byte action. 234 * 235 * Returns the corresponding C++ I2CCompareByteAction object. 236 * 237 * Throws an exception if parsing fails. 238 * 239 * @param element JSON element 240 * @return I2CCompareByteAction object 241 */ 242 std::unique_ptr<I2CCompareByteAction> parseI2CCompareByte( 243 const nlohmann::json& element); 244 245 /** 246 * Parses a JSON element containing an i2c_compare_bytes action. 247 * 248 * Returns the corresponding C++ I2CCompareBytesAction object. 249 * 250 * Throws an exception if parsing fails. 251 * 252 * @param element JSON element 253 * @return I2CCompareBytesAction object 254 */ 255 std::unique_ptr<I2CCompareBytesAction> parseI2CCompareBytes( 256 const nlohmann::json& element); 257 258 /** 259 * Parses a JSON element containing an i2c_interface. 260 * 261 * Returns the corresponding C++ i2c::I2CInterface object. 262 * 263 * Throws an exception if parsing fails. 264 * 265 * @param element JSON element 266 * @return i2c::I2CInterface object 267 */ 268 std::unique_ptr<i2c::I2CInterface> parseI2CInterface( 269 const nlohmann::json& element); 270 271 /** 272 * Parses a JSON element containing an i2c_write_bit action. 273 * 274 * Returns the corresponding C++ I2CWriteBitAction object. 275 * 276 * Throws an exception if parsing fails. 277 * 278 * @param element JSON element 279 * @return I2CWriteBitAction object 280 */ 281 std::unique_ptr<I2CWriteBitAction> parseI2CWriteBit( 282 const nlohmann::json& element); 283 284 /** 285 * Parses a JSON element containing an i2c_write_byte action. 286 * 287 * Returns the corresponding C++ I2CWriteByteAction object. 288 * 289 * Throws an exception if parsing fails. 290 * 291 * @param element JSON element 292 * @return I2CWriteByteAction object 293 */ 294 std::unique_ptr<I2CWriteByteAction> parseI2CWriteByte( 295 const nlohmann::json& element); 296 297 /** 298 * Parses a JSON element containing an i2c_write_bytes action. 299 * 300 * Returns the corresponding C++ I2CWriteBytesAction object. 301 * 302 * Throws an exception if parsing fails. 303 * 304 * @param element JSON element 305 * @return I2CWriteBytesAction object 306 */ 307 std::unique_ptr<I2CWriteBytesAction> parseI2CWriteBytes( 308 const nlohmann::json& element); 309 310 /** 311 * Parses a JSON element containing an if action. 312 * 313 * Returns the corresponding C++ IfAction object. 314 * 315 * Throws an exception if parsing fails. 316 * 317 * @param element JSON element 318 * @return IfAction object 319 */ 320 std::unique_ptr<IfAction> parseIf(const nlohmann::json& element); 321 322 /** 323 * Parses a JSON element containing a relative inventory path. 324 * 325 * Returns the corresponding C++ string containing the absolute inventory path. 326 * 327 * Inventory paths in the JSON configuration file are relative. Adds the 328 * necessary prefix to make the path absolute. 329 * 330 * Throws an exception if parsing fails. 331 * 332 * @param element JSON element 333 * @return absolute D-Bus inventory path 334 */ 335 std::string parseInventoryPath(const nlohmann::json& element); 336 337 /** 338 * Parses a JSON element containing a log_phase_fault action. 339 * 340 * Returns the corresponding C++ LogPhaseFaultAction object. 341 * 342 * Throws an exception if parsing fails. 343 * 344 * @param element JSON element 345 * @return LogPhaseFaultAction object 346 */ 347 std::unique_ptr<LogPhaseFaultAction> parseLogPhaseFault( 348 const nlohmann::json& element); 349 350 /** 351 * Parses a JSON element containing a not action. 352 * 353 * Returns the corresponding C++ NotAction object. 354 * 355 * Throws an exception if parsing fails. 356 * 357 * @param element JSON element 358 * @return NotAction object 359 */ 360 std::unique_ptr<NotAction> parseNot(const nlohmann::json& element); 361 362 /** 363 * Parses a JSON element containing an or action. 364 * 365 * Returns the corresponding C++ OrAction object. 366 * 367 * Throws an exception if parsing fails. 368 * 369 * @param element JSON element 370 * @return OrAction object 371 */ 372 std::unique_ptr<OrAction> parseOr(const nlohmann::json& element); 373 374 /** 375 * Parses a JSON element containing a phase_fault_detection object. 376 * 377 * Returns the corresponding C++ PhaseFaultDetection object. 378 * 379 * Throws an exception if parsing fails. 380 * 381 * @param element JSON element 382 * @return PhaseFaultDetection object 383 */ 384 std::unique_ptr<PhaseFaultDetection> parsePhaseFaultDetection( 385 const nlohmann::json& element); 386 387 /** 388 * Parses a JSON element containing a PhaseFaultType expressed as a string. 389 * 390 * Returns the corresponding PhaseFaultType enum value. 391 * 392 * Throws an exception if parsing fails. 393 * 394 * @param element JSON element 395 * @return PhaseFaultType enum value 396 */ 397 PhaseFaultType parsePhaseFaultType(const nlohmann::json& element); 398 399 /** 400 * Parses a JSON element containing a pmbus_read_sensor action. 401 * 402 * Returns the corresponding C++ PMBusReadSensorAction object. 403 * 404 * Throws an exception if parsing fails. 405 * 406 * @param element JSON element 407 * @return PMBusReadSensorAction object 408 */ 409 std::unique_ptr<PMBusReadSensorAction> parsePMBusReadSensor( 410 const nlohmann::json& element); 411 412 /** 413 * Parses a JSON element containing a pmbus_write_vout_command action. 414 * 415 * Returns the corresponding C++ PMBusWriteVoutCommandAction object. 416 * 417 * Throws an exception if parsing fails. 418 * 419 * @param element JSON element 420 * @return PMBusWriteVoutCommandAction object 421 */ 422 std::unique_ptr<PMBusWriteVoutCommandAction> parsePMBusWriteVoutCommand( 423 const nlohmann::json& element); 424 425 /** 426 * Parses a JSON element containing a presence_detection object. 427 * 428 * Returns the corresponding C++ PresenceDetection object. 429 * 430 * Throws an exception if parsing fails. 431 * 432 * @param element JSON element 433 * @return PresenceDetection object 434 */ 435 std::unique_ptr<PresenceDetection> parsePresenceDetection( 436 const nlohmann::json& element); 437 438 /** 439 * Parses a JSON element containing a rail. 440 * 441 * Returns the corresponding C++ Rail object. 442 * 443 * Throws an exception if parsing fails. 444 * 445 * @param element JSON element 446 * @return Rail object 447 */ 448 std::unique_ptr<Rail> parseRail(const nlohmann::json& element); 449 450 /** 451 * Parses a JSON element containing an array of rails. 452 * 453 * Returns the corresponding C++ Rail objects. 454 * 455 * Throws an exception if parsing fails. 456 * 457 * @param element JSON element 458 * @return vector of Rail objects 459 */ 460 std::vector<std::unique_ptr<Rail>> parseRailArray( 461 const nlohmann::json& element); 462 463 /** 464 * Parses the JSON root element of the entire configuration file. 465 * 466 * Returns the corresponding C++ Rule and Chassis objects. 467 * 468 * Throws an exception if parsing fails. 469 * 470 * @param element JSON element 471 * @return tuple containing vectors of Rule and Chassis objects 472 */ 473 std::tuple<std::vector<std::unique_ptr<Rule>>, 474 std::vector<std::unique_ptr<Chassis>>> 475 parseRoot(const nlohmann::json& element); 476 477 /** 478 * Parses a JSON element containing a rule. 479 * 480 * Returns the corresponding C++ Rule object. 481 * 482 * Throws an exception if parsing fails. 483 * 484 * @param element JSON element 485 * @return Rule object 486 */ 487 std::unique_ptr<Rule> parseRule(const nlohmann::json& element); 488 489 /** 490 * Parses a JSON element containing an array of rules. 491 * 492 * Returns the corresponding C++ Rule objects. 493 * 494 * Throws an exception if parsing fails. 495 * 496 * @param element JSON element 497 * @return vector of Rule objects 498 */ 499 std::vector<std::unique_ptr<Rule>> parseRuleArray( 500 const nlohmann::json& element); 501 502 /** 503 * Parses the "rule_id" or "actions" property in a JSON element. 504 * 505 * The element must contain one property or the other but not both. 506 * 507 * If the element contains a "rule_id" property, the corresponding C++ 508 * RunRuleAction object is returned. 509 * 510 * If the element contains an "actions" property, the corresponding C++ Action 511 * objects are returned. 512 * 513 * Throws an exception if parsing fails. 514 * 515 * @param element JSON element 516 * @return vector of Action objects 517 */ 518 std::vector<std::unique_ptr<Action>> parseRuleIDOrActionsProperty( 519 const nlohmann::json& element); 520 521 /** 522 * Parses a JSON element containing a run_rule action. 523 * 524 * Returns the corresponding C++ RunRuleAction object. 525 * 526 * Throws an exception if parsing fails. 527 * 528 * @param element JSON element 529 * @return RunRuleAction object 530 */ 531 std::unique_ptr<RunRuleAction> parseRunRule(const nlohmann::json& element); 532 533 /** 534 * Parses a JSON element containing a SensorDataFormat expressed as a string. 535 * 536 * Returns the corresponding SensorDataFormat enum value. 537 * 538 * Throws an exception if parsing fails. 539 * 540 * @param element JSON element 541 * @return SensorDataFormat enum value 542 */ 543 pmbus_utils::SensorDataFormat parseSensorDataFormat( 544 const nlohmann::json& element); 545 546 /** 547 * Parses a JSON element containing a sensor_monitoring object. 548 * 549 * Returns the corresponding C++ SensorMonitoring object. 550 * 551 * Throws an exception if parsing fails. 552 * 553 * @param element JSON element 554 * @return SensorMonitoring object 555 */ 556 std::unique_ptr<SensorMonitoring> parseSensorMonitoring( 557 const nlohmann::json& element); 558 559 /** 560 * Parses a JSON element containing a SensorType expressed as a string. 561 * 562 * Returns the corresponding SensorType enum value. 563 * 564 * Throws an exception if parsing fails. 565 * 566 * @param element JSON element 567 * @return SensorType enum value 568 */ 569 SensorType parseSensorType(const nlohmann::json& element); 570 571 /** 572 * Parses a JSON element containing a set_device action. 573 * 574 * Returns the corresponding C++ SetDeviceAction object. 575 * 576 * Throws an exception if parsing fails. 577 * 578 * @param element JSON element 579 * @return SetDeviceAction object 580 */ 581 std::unique_ptr<SetDeviceAction> parseSetDevice(const nlohmann::json& element); 582 583 /** 584 * Parses a JSON element containing a VoutDataFormat expressed as a string. 585 * 586 * Returns the corresponding VoutDataFormat enum value. 587 * 588 * Throws an exception if parsing fails. 589 * 590 * @param element JSON element 591 * @return VoutDataFormat enum value 592 */ 593 pmbus_utils::VoutDataFormat parseVoutDataFormat(const nlohmann::json& element); 594 595 } // namespace internal 596 597 } // namespace phosphor::power::regulators::config_file_parser 598