LCOV - code coverage report
Current view: top level - coll_communicator_mgr/rank_graph/common - json_parser.cc (source / functions) Coverage Total Hit
Test: coverage.info Lines: 46.0 % 63 29
Test Date: 2026-08-18 17:47:01 Functions: 60.0 % 5 3

            Line data    Source code
       1              : /**
       2              :  * Copyright (c) 2025 Huawei Technologies Co., Ltd.
       3              :  * This program is free software, you can redistribute it and/or modify it under the terms and conditions of
       4              :  * CANN Open Software License Agreement Version 2.0 (the "License").
       5              :  * Please refer to the License for details. You may not use this file except in compliance with the License.
       6              :  * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED,
       7              :  * INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE.
       8              :  * See LICENSE in the root of the software repository for the full text of the License.
       9              :  */
      10              : 
      11              : #include <climits>
      12              : #include "json_parser.h"
      13              : #include "adapter_error_manager_pub.h"
      14              : 
      15              : namespace Hccl {
      16              : 
      17          535 : std::string GetJsonProperty(const nlohmann::json& obj, const char* propName, bool required)
      18              : {
      19          535 :     if (propName == nullptr) {
      20            0 :         std::string msg = StringFormat("[%s] propName is nullptr", __func__);
      21            0 :         THROW<NullPtrException>(msg);
      22            0 :     }
      23          535 :     if (!required) {
      24           26 :         return obj.value<std::string>(propName, "");
      25              :     }
      26              : 
      27          522 :     if (!obj.contains(propName)) {
      28           16 :         RPT_INPUT_ERR(
      29              :             true, "EI0017", std::vector<std::string>({"config"}), std::vector<std::string>({std::string(propName)}));
      30            2 :         THROW<InvalidParamsException>(
      31            6 :             StringFormat("[Get][JsonProperty] json object does not contain property[%s].", propName));
      32              :     }
      33          520 :     std::string value = obj.at(propName).get<std::string>();
      34          520 :     return value;
      35          524 : }
      36              : 
      37          551 : u32 GetJsonPropertyUInt(const nlohmann::json& obj, const char* propName, bool required, u32 defaultValue)
      38              : {
      39          551 :     if (!required) {
      40           36 :         return obj.value<u32>(propName, defaultValue);
      41              :     }
      42              : 
      43          515 :     if (!obj.contains(propName)) {
      44           40 :         RPT_INPUT_ERR(
      45              :             true, "EI0017", std::vector<std::string>({"config"}), std::vector<std::string>({std::string(propName)}));
      46            5 :         THROW<InvalidParamsException>(
      47           15 :             StringFormat("[Get][JsonPropertyUInt] json object does not contain property[%s].", propName));
      48              :     }
      49              : 
      50          510 :     s64 value = obj.at(propName).get<s64>();
      51          509 :     if (value < 0 || value > UINT32_MAX) {
      52           28 :         RPT_INPUT_ERR(
      53              :             true, "EI0014", std::vector<std::string>({"value", "variable", "expect"}),
      54              :             std::vector<std::string>({std::to_string(value), std::string(propName), "0 ~ UINT32_MAX"}));
      55            2 :         THROW<InvalidParamsException>(StringFormat(
      56              :             "[Get][JsonPropertyUInt]errNo[0x%016llx]:json object "
      57              :             "property value of Name[%s] should be an unsigned 32-bit integer but acutally not!",
      58              :             HCOM_ERROR_CODE(HcclResult::HCCL_E_PARA), propName));
      59              :     }
      60          507 :     return static_cast<u32>(value);
      61           14 : }
      62              : 
      63            0 : s32 GetJsonPropertySInt(const nlohmann::json& obj, const char* propName, bool required, s32 defaultValue)
      64              : {
      65            0 :     if (!required) {
      66            0 :         return obj.value<s32>(propName, defaultValue);
      67              :     }
      68              : 
      69            0 :     if (!obj.contains(propName)) {
      70            0 :         RPT_INPUT_ERR(
      71              :             true, "EI0017", std::vector<std::string>({"config"}), std::vector<std::string>({std::string(propName)}));
      72            0 :         THROW<InvalidParamsException>(
      73            0 :             StringFormat("[Get][JsonPropertySInt] json object does not contain property[%s].", propName));
      74              :     }
      75              : 
      76            0 :     s64 value = obj.at(propName).get<s64>();
      77            0 :     if (value < INT_MIN || value > INT_MAX) {
      78            0 :         RPT_INPUT_ERR(
      79              :             true, "EI0014", std::vector<std::string>({"value", "variable", "expect"}),
      80              :             std::vector<std::string>({std::to_string(value), std::string(propName), "0 ~ INT_MAX"}));
      81            0 :         THROW<InvalidParamsException>(StringFormat(
      82              :             "[Get][JsonPropertySInt]errNo[0x%016llx]:json object "
      83              :             "property value of Name[%s] is not signed number!",
      84              :             HCOM_ERROR_CODE(HcclResult::HCCL_E_PARA), propName));
      85              :     }
      86            0 :     HCCL_INFO(
      87              :         "[Get][JsonPropertySInt]json object property value of Name [%s] is [%d]", propName, static_cast<s32>(value));
      88            0 :     return static_cast<s32>(value);
      89            0 : }
      90              : 
      91          314 : void GetJsonPropertyList(const nlohmann::json& obj, const char* propName, nlohmann::json& listObj)
      92              : {
      93          314 :     if (!obj.contains(propName)) {
      94            0 :         RPT_INPUT_ERR(
      95              :             true, "EI0017", std::vector<std::string>({"config"}), std::vector<std::string>({std::string(propName)}));
      96            0 :         THROW<InvalidParamsException>(
      97            0 :             StringFormat("[Get][JsonPropertyList] json object does not contain property[%s].", propName));
      98              :     }
      99              : 
     100          314 :     listObj = obj.at(propName);
     101          314 :     if (!listObj.is_array()) {
     102            0 :         RPT_INPUT_ERR(
     103              :             true, "EI0014", std::vector<std::string>({"value", "variable", "expect"}),
     104              :             std::vector<std::string>({std::string(listObj.type_name()), std::string(propName), "array"}));
     105            0 :         THROW<InvalidParamsException>(StringFormat(
     106              :             "[Get][GetJsonPropertyList]errNo[0x%016llx]:json object "
     107              :             "property value of Name \"%s\" is not list!",
     108              :             HCOM_ERROR_CODE(HcclResult::HCCL_E_PARA), propName));
     109              :     }
     110          314 : }
     111              : 
     112            0 : void JsonParser::ParseFileToJson(const std::string& filePath, nlohmann::json& parseInformation) const
     113              : {
     114              :     // 校验文件是否存在
     115            0 :     char resolvedPath[PATH_MAX] = {0};
     116            0 :     if (realpath(filePath.c_str(), resolvedPath) == nullptr) {
     117            0 :         RPT_INPUT_ERR(
     118              :             true, "EI0004", std::vector<std::string>({"error_reason", "ranktable_path"}),
     119              :             std::vector<std::string>(
     120              :                 {filePath, "The rankTable file path does not exist, the permission is insufficient, or the JSON format "
     121              :                            "is incorrect."}));
     122            0 :         THROW<InvalidParamsException>(StringFormat(
     123              :             "[Get][RanktableRealPath]errNo[0x%016llx] path %s is not a valid real path",
     124              :             HCOM_ERROR_CODE(HcclResult::HCCL_E_PARA), filePath.c_str()));
     125              :     }
     126              : 
     127            0 :     HCCL_INFO("waiting for json file load complete");
     128            0 :     std::ifstream infoFile(resolvedPath, std::ifstream::in);
     129            0 :     if (!infoFile) {
     130            0 :         THROW<InternalException>(StringFormat(
     131              :             "[Read][File]errNo[0x%016llx],open file %s failed", HCOM_ERROR_CODE(HcclResult::HCCL_E_OPEN_FILE_FAILURE),
     132              :             resolvedPath));
     133              :     }
     134              : 
     135            0 :     ParseInformation(parseInformation, infoFile);
     136            0 :     infoFile.close();
     137            0 : }
     138              : 
     139              : } // namespace Hccl
        

Generated by: LCOV version 2.0-1