LCOV - code coverage report
Current view: top level - legacy/ascend910/framework/common/src/topo - topoinfo_ranktableParser.cc (source / functions) Coverage Total Hit
Test: coverage.info Lines: 62.2 % 270 168
Test Date: 2026-07-28 12:11:00 Functions: 75.9 % 29 22

            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 "topoinfo_ranktableParser_pub.h"
      12              : 
      13              : #include <map>
      14              : #include <vector>
      15              : #include <string>
      16              : #include <fstream>
      17              : #include <algorithm>
      18              : #include <unistd.h>
      19              : #include <chrono>
      20              : #include <iostream>
      21              : #include <arpa/inet.h>
      22              : 
      23              : #include "adapter_error_manager_pub.h"
      24              : #include "json_utils.h"
      25              : 
      26              : using namespace std;
      27              : using namespace hccl;
      28              : 
      29              : constexpr char TYPE_IP[] = "ip";
      30              : constexpr char TYPE_NAME[] = "Name";
      31              : constexpr char TYPE_ETH_NAME[] = "ethName";
      32              : 
      33              : constexpr u32 SERVERID_MIN = 0;         // 整形serverId最小值
      34              : constexpr u32 SERVERID_MAX = 0xFFFFFFFF;     // 整形serverId最大值
      35              : 
      36              : constexpr u32 HCCL_RANKTABLE_TIMEOUT_S = (30 * 60); // 读取ranktable json文件超时时间30 * 60s
      37              : 
      38              : const std::map<JsonUniqueInfoType, std::string> JsonInfoTypeNameMap = {
      39              :     {JsonUniqueInfoType::UNIQUE_INFO_TYPE_DEVICE_IP, "device_ip"},
      40              :     {JsonUniqueInfoType::UNIQUE_INFO_TYPE_BACKUP_DEVICE_IP, "backup_device_ip"},
      41              :     {JsonUniqueInfoType::UNIQUE_INFO_TYPE_SERVER_ID, "server_id"},
      42              :     {JsonUniqueInfoType::UNIQUE_INFO_TYPE_ETH_IP, "eth_ip"},
      43              :     {JsonUniqueInfoType::UNIQUE_INFO_TYPE_GROUP_NAME, "group_name"},
      44              :     {JsonUniqueInfoType::UNIQUE_INFO_TYPE_POD_NAME, "pod_name"},
      45              :     {JsonUniqueInfoType::UNIQUE_INFO_TYPE_ETH_NAME, "eth_name"},
      46              :     {JsonUniqueInfoType::UNIQUE_INFO_TYPE_SUPER_POD_ID, "super_pod_id"},
      47              :     {JsonUniqueInfoType::UNIQUE_INFO_NUM, "info_num"},
      48              : };
      49              : 
      50          390 : bool CheckFilePath(const std::string &filePath, const std::string &fileType)
      51              : {
      52          390 :     HCCL_DEBUG("file_path:%s,file_type:%s", filePath.c_str(), fileType.c_str());
      53              : 
      54              :     /* 如果file_path是file_type类型的文件,则file_path是以file_type结尾的 */
      55          390 :     return filePath.find(fileType) + fileType.length() == filePath.length();
      56              : }
      57              : 
      58          390 : HcclResult GetFileName(const std::string &filePath, const std::string &fileType, std::string &fileName)
      59              : {
      60          390 :     HCCL_DEBUG("file_path:%s,file_type:%s", filePath.c_str(), fileType.c_str());
      61              : 
      62          390 :     auto fi = filePath.find_last_of('/');
      63          390 :     if (fi != std::string::npos) {
      64          390 :         fileName = filePath.substr(fi + 1);
      65          390 :         fileName = fileName.substr(0, fileName.length() - fileType.length());
      66              :     }
      67              : 
      68          390 :     HCCL_DEBUG("get file_name:%s", fileName.c_str());
      69              : 
      70          390 :     return fileName.empty() ? HCCL_E_PARA : HCCL_SUCCESS;
      71              : }
      72              : 
      73          387 : HcclResult CheckAverageDev(u32 uDeviceNum, u32 uServerNum)
      74              : {
      75          387 :     if (uServerNum == 0) {
      76            0 :         HCCL_ERROR("[Check][AverageDev]errNo[0x%016llx] server num is zero", HCOM_ERROR_CODE(HCCL_E_PARA));
      77            0 :         return HCCL_E_PARA;
      78              :     }
      79              :     // check the valid of average device per sever
      80          387 :     u32 averageDevice = uDeviceNum / uServerNum;
      81              : 
      82              :     // 如果device_num/server_num 不能被整除,报错
      83              :     // averageDevice不等于1 ,2,4,8... 报错
      84          387 :     if ((uDeviceNum % uServerNum) ||
      85          387 :         ((averageDevice != AVERAGE_DEVICE_SIXTEEN) &&
      86          347 :          (averageDevice != AVERAGE_DEVICE_EIGHT) &&
      87          221 :          (averageDevice != AVERAGE_DEVICE_FOUR)  &&
      88          181 :          (averageDevice != AVERAGE_DEVICE_TWO)   &&
      89              :          (averageDevice != AVERAGE_DEVICE_ONE))) {
      90            0 :         HCCL_ERROR("[Check][AverageDev]errNo[0x%016llx] device / server is invalid, uDeviceNum[%u], uServerNum[%u]",
      91              :             HCOM_ERROR_CODE(HCCL_E_PARA), uDeviceNum, uServerNum);
      92            0 :         return HCCL_E_PARA;
      93              :     }
      94              : 
      95          387 :     return HCCL_SUCCESS;
      96              : }
      97              : 
      98         1170 : TopoInfoRanktableParser::TopoInfoRanktableParser(const std::string &rankTableM, const std::string &identify)
      99         2340 :     : rankTableFile_(rankTableM), identify_(identify), statusCompleted_(false),
     100         5850 :       uniqueInfoCheckPool_(static_cast<u32>(JsonUniqueInfoType::UNIQUE_INFO_NUM)), devMap_()
     101              : {
     102         1169 : }
     103              : 
     104         1170 : TopoInfoRanktableParser::~TopoInfoRanktableParser()
     105              : {
     106         1170 : }
     107              : 
     108          390 : HcclResult TopoInfoRanktableParser::ReadFile(const std::string &readFile)
     109              : {
     110          390 :     std::string fileType = ".json";
     111              :     // 获取文件名字
     112          390 :     HcclResult ret = GetFileName(readFile, fileType, fileName_);
     113          390 :     CHK_PRT_RET(ret != HCCL_SUCCESS, HCCL_ERROR("[Read][File]get file[%s] name error", readFile.c_str()), HCCL_E_PARA);
     114              : 
     115              :     // 已只读方式打开该文件
     116          390 :     std::ifstream infile(readFile.c_str(), std::ifstream::in);
     117          390 :     if (!infile) {
     118            0 :         RPT_INPUT_ERR(true,
     119              :                 "EI0004",
     120              :                 std::vector<std::string>({"ranktable_path", "error_reason"}),
     121              :                 std::vector<std::string>({readFile.c_str(), "failed to open the file in read-only mode"}));
     122            0 :         HCCL_ERROR("[%s][%s]errNo[0x%016llx],open file %s failed", LOG_KEYWORDS_INIT_GROUP.c_str(),
     123              :             LOG_KEYWORDS_RANKTABLE_CONFIG.c_str(), HCOM_ERROR_CODE(HCCL_E_INTERNAL), readFile.c_str());
     124            0 :         return HCCL_E_INTERNAL;
     125              :     } else {
     126          390 :         fileContent_.clear();
     127              :         try {
     128          390 :             infile >> fileContent_; // 将文件内容读取到json对象内
     129            0 :         } catch (...) {
     130            0 :             RPT_INPUT_ERR(true,
     131              :                 "EI0004",
     132              :                 std::vector<std::string>({"ranktable_path", "error_reason"}),
     133              :                 std::vector<std::string>({readFile.c_str(), RANKTABLE_PARSE_ERROR_REASON}));
     134              : 
     135            0 :             HCCL_ERROR("[%s][%s]errNo[0x%016llx] load file[%s] to json fail. please check json file!",
     136              :                 LOG_KEYWORDS_INIT_GROUP.c_str(),
     137              :                 LOG_KEYWORDS_RANKTABLE_CONFIG.c_str(),
     138              :                 HCOM_ERROR_CODE(HCCL_E_INTERNAL),
     139              :                 readFile.c_str());
     140            0 :             infile.close();
     141            0 :             return HCCL_E_INTERNAL;
     142            0 :         }
     143              :     }
     144          390 :     infile.close();
     145          390 :     return HCCL_SUCCESS;
     146          390 : }
     147              : 
     148          390 : HcclResult TopoInfoRanktableParser::LoadFile(const std::string &file)
     149              : {
     150          390 :     if (file.empty()) {
     151            0 :         HCCL_ERROR("[%s][%s]errNo[0x%016llx] json file length is zero", LOG_KEYWORDS_INIT_GROUP.c_str(),
     152              :             LOG_KEYWORDS_RANKTABLE_CONFIG.c_str(), HCOM_ERROR_CODE(HCCL_E_PARA));
     153            0 :         return HCCL_E_PARA;
     154              :     }
     155          390 :     std::string fileType = ".json";
     156              : 
     157          390 :     if (!CheckFilePath(file, fileType)) {
     158            0 :         HCCL_ERROR("[%s][%s]errNo[0x%016llx] path %s is not a valid %s file", LOG_KEYWORDS_INIT_GROUP.c_str(),
     159              :             LOG_KEYWORDS_RANKTABLE_CONFIG.c_str(), HCOM_ERROR_CODE(HCCL_E_INTERNAL), file.c_str(), fileType.c_str());
     160            0 :         return HCCL_E_INTERNAL;
     161              :     }
     162              :     // 打开该文件前,判断该文件路径是否有效 规范
     163          390 :     char realFile[PATH_MAX] = {0};
     164          390 :     if (realpath(file.c_str(), realFile) == nullptr) {
     165            0 :         HCCL_ERROR("[%s][%s]errNo[0x%016llx] path %s is not a valid real path", LOG_KEYWORDS_INIT_GROUP.c_str(),
     166              :             LOG_KEYWORDS_RANKTABLE_CONFIG.c_str(), HCOM_ERROR_CODE(HCCL_E_PARA), file.c_str());
     167            0 :         return HCCL_E_PARA;
     168              :     }
     169          390 :     string strFilePath = std::string(realFile);
     170          390 :     const std::chrono::seconds TIMEOUT(HCCL_RANKTABLE_TIMEOUT_S);
     171          390 :     auto startTime = std::chrono::steady_clock::now();
     172              : 
     173              :     // load file and check the status
     174          390 :     HCCL_INFO("waiting for ranktable load complete...");
     175              : 
     176              :     // set timeout for status of file to be completed
     177              :     // 实验室场景下总是completed,cloud场景初始填入initlizing,kube补充完整后成为completed状态
     178              :     do {
     179          390 :         if ((std::chrono::steady_clock::now() - startTime) >= TIMEOUT) {
     180            0 :             HCCL_ERROR("[%s][%s]errNo[0x%016llx] Load ranktable file[%s] timeout[%lld]s",
     181              :                 LOG_KEYWORDS_INIT_GROUP.c_str(), LOG_KEYWORDS_RANKTABLE_CONFIG.c_str(), HCOM_ERROR_CODE(HCCL_E_TIMEOUT),
     182              :                 strFilePath.c_str(), TIMEOUT);
     183            0 :             return HCCL_E_TIMEOUT;
     184              :         }
     185              :         // 读取文件内容
     186          390 :         HcclResult ret = ReadFile(strFilePath);
     187          390 :         CHK_PRT_RET(ret != HCCL_SUCCESS,
     188              :             HCCL_ERROR("[%s][%s]read file[%s] error", LOG_KEYWORDS_INIT_GROUP.c_str(),
     189              :                 LOG_KEYWORDS_RANKTABLE_CONFIG.c_str(), strFilePath.c_str()), HCCL_E_PARA);
     190              : 
     191          390 :         CHK_RET(RefreshStatus());
     192              : 
     193          389 :         if (IsReady()) {
     194          389 :             break;
     195              :         }
     196              : 
     197            0 :         SalSleep(1); // 每间隔一段时间去检测文件是否ready
     198            0 :     } while (true);
     199              : 
     200          389 :     HCCL_INFO("ranktable is ready");
     201          389 :     return HCCL_SUCCESS;
     202          390 : }
     203              : 
     204          774 : HcclResult TopoInfoRanktableParser::LoadRankTableString(const std::string &string)
     205              : {
     206          774 :     CHK_RET(LoadString(string));
     207          774 :     fileName_ = "rankTableString";
     208          774 :     CHK_RET(RefreshStatus());
     209              : 
     210          774 :     HCCL_INFO("ranktable is ready");
     211          774 :     return HCCL_SUCCESS;
     212              : }
     213              : 
     214            0 : HcclResult TopoInfoRanktableParser::LoadConfigString(const std::string &string)
     215              : {
     216            0 :     CHK_RET(LoadString(string));
     217            0 :     fileName_ = "configString";
     218              : 
     219            0 :     HCCL_INFO("ranktable is ready");
     220            0 :     return HCCL_SUCCESS;
     221              : }
     222              : 
     223          774 : HcclResult TopoInfoRanktableParser::LoadString(const std::string &string)
     224              : {
     225              :     // 入参检查
     226          774 :     if (string.empty()) {
     227            0 :         RPT_INPUT_ERR(true,
     228              :         "EI0004",
     229              :         std::vector<std::string>({"ranktable_path", "error_reason"}),
     230              :         std::vector<std::string>({string, "rankTable path is nullptr"}));
     231            0 :         HCCL_ERROR("[%s][%s]errNo[0x%016llx] json string length is zero", LOG_KEYWORDS_INIT_GROUP.c_str(),
     232              :             LOG_KEYWORDS_RANKTABLE_CONFIG.c_str(), HCOM_ERROR_CODE(HCCL_E_PARA));
     233            0 :         return HCCL_E_PARA;
     234              :     }
     235          774 :     HCCL_INFO("waiting for json string load complete...");
     236              :     // 将字符串内容读取到json对象中
     237          774 :     HcclResult ret = JsonUtils::ParseInformation(fileContent_, string);
     238          774 :     RPT_INPUT_ERR(ret != HCCL_SUCCESS,
     239              :         "EI0004",
     240              :         std::vector<std::string>({"ranktable_path", "error_reason"}),
     241              :         std::vector<std::string>({string, "failed to parse rank table JSON content, the JSON format is incorrect"}));
     242          774 :     CHK_PRT_RET(ret != HCCL_SUCCESS,
     243              :         HCCL_ERROR("[%s][%s]Failed to read the string %s into JSON.", LOG_KEYWORDS_INIT_GROUP.c_str(),
     244              :             LOG_KEYWORDS_RANKTABLE_CONFIG.c_str(), string.c_str()), ret);
     245          774 :     return HCCL_SUCCESS;
     246            0 : }
     247              : 
     248            0 : HcclResult TopoInfoRanktableParser::GetClusterInfo(RankTable_t &clusterInfo)
     249              : {
     250            0 :     return HCCL_SUCCESS;
     251              : }
     252            0 : HcclResult TopoInfoRanktableParser::GetClusterInfo(hccl::HcclCommParams &params,
     253              :     hccl::RankTable_t &rankTable)
     254              : {
     255            0 :     return HCCL_SUCCESS;
     256              : }
     257              : 
     258          387 : HcclResult TopoInfoRanktableParser::Init()
     259              : {
     260          387 :     HcclResult ret = LoadRankTableString(rankTableFile_);
     261          387 :     CHK_PRT_RET(ret != HCCL_SUCCESS,
     262              :         HCCL_ERROR("[Init][TopoInfoRanktableParser]load string[%s] error", fileName_.c_str()), ret);
     263              : 
     264          387 :     return HCCL_SUCCESS;
     265              : }
     266              : 
     267          390 : HcclResult TopoInfoRanktableParser::LoadFileInit(std::string &rankTableM)
     268              : {
     269          390 :     HcclResult ret = LoadFile(rankTableFile_);
     270          399 :     RPT_INPUT_ERR(ret != HCCL_SUCCESS,
     271              :         "EI0004",
     272              :         std::vector<std::string>({"ranktable_path", "error_reason"}),
     273              :         std::vector<std::string>({rankTableFile_, RANKTABLE_PARSE_ERROR_REASON}));
     274              : 
     275          390 :     CHK_PRT_RET(ret != HCCL_SUCCESS,
     276              :         HCCL_ERROR("[%s][%s]load file[%s] error", LOG_KEYWORDS_INIT_GROUP.c_str(),
     277              :             LOG_KEYWORDS_RANKTABLE_CONFIG.c_str(), rankTableFile_.c_str()), ret);
     278              : 
     279          389 :     rankTableM = fileContent_.dump();
     280          388 :     return HCCL_SUCCESS;
     281            1 : }
     282              : 
     283            0 : HcclResult TopoInfoRanktableParser::SetIsInterSuperPodRetryEnable(bool isRetryEnable)
     284              : {
     285            0 :     return HCCL_SUCCESS;
     286              : }
     287              : 
     288          387 : HcclResult TopoInfoRanktableParser::GetRanktableVersion(std::string &version)
     289              : {
     290              :     /* 查找json对象中是否有该属性, 不存在的属性不能直接访问 */
     291          387 :     if (fileContent_.find(CLUSTER_PROP_VERSION) == fileContent_.end()) {
     292          387 :         version = "Standard";
     293          387 :         HCCL_DEBUG("RankTableVersion is not found, Parser: %s", version.c_str());
     294              :     } else {
     295            0 :         CHK_RET(GetJsonProperty(fileContent_, CLUSTER_PROP_VERSION, version_, false));
     296            0 :         version = version_;
     297            0 :         HCCL_DEBUG("%s.json -> version: %s", fileName_.c_str(), version.c_str());
     298              :     }
     299          387 :     return HCCL_SUCCESS;
     300              : }
     301              : 
     302         1164 : HcclResult TopoInfoRanktableParser::RefreshStatus()
     303              : {
     304         1164 :     std::string status = "";
     305         1164 :     CHK_RET(GetJsonProperty(fileContent_, "status", status, false));
     306         1163 :     HCCL_DEBUG("%s.json -> status: %s", fileName_.c_str(), status.c_str());
     307         1163 :     statusCompleted_ = (status == "completed");
     308         1163 :     return HCCL_SUCCESS;
     309         1164 : }
     310              : 
     311          389 : bool TopoInfoRanktableParser::IsReady() const
     312              : {
     313          389 :     return this->statusCompleted_;
     314              : }
     315              : 
     316         1938 : HcclResult TopoInfoRanktableParser::GetJsonProperty(const nlohmann::json &obj, const char *propName,
     317              :     std::string &propValue, bool optionalProp) const
     318              : {
     319              :     /* 查找json对象中是否有该属性, 不存在的属性不能直接访问 */
     320         1938 :     if (obj.find(propName) == obj.end()) {
     321            1 :         if (optionalProp) {
     322            0 :             HCCL_WARNING("json object has no property called %s", propName);
     323              :         } else {
     324            8 :             RPT_INPUT_ERR(true,
     325              :                 "EI0017",
     326              :                 std::vector<std::string>({"config"}),
     327              :                 std::vector<std::string>({std::string(propName)}));
     328            1 :             HCCL_ERROR("[%s][%s]json object has no property called %s", LOG_KEYWORDS_INIT_GROUP.c_str(),
     329              :                 LOG_KEYWORDS_RANKTABLE_CHECK.c_str(), propName);
     330              :         }
     331            1 :         return HCCL_E_NOT_FOUND;
     332              :     }
     333              : 
     334         1936 :     if (obj[propName].is_string()) {
     335         1935 :         propValue = obj[propName];
     336         1937 :         return HCCL_SUCCESS;
     337              :     } else {
     338            0 :         RPT_INPUT_ERR(true,
     339              :             "EI0017",
     340              :             std::vector<std::string>({"config"}),
     341              :             std::vector<std::string>({std::string(propName)}));
     342            0 :         HCCL_ERROR("[Get][JsonProperty]errNo[0x%016llx] json object property value of Name[%s] is not string!",
     343              :             HCOM_ERROR_CODE(HCCL_E_PARA), propName);
     344            0 :         return HCCL_E_PARA;
     345              :     }
     346            2 : }
     347              : 
     348          386 : HcclResult TopoInfoRanktableParser::GetJsonProperty(const nlohmann::json &obj, const char *propName,
     349              :     nlohmann::json &propValue, bool optionalProp) const
     350              : {
     351              :     /* 查找json对象中是否有该属性, 不存在的属性不能直接访问 */
     352          386 :     if (obj.find(propName) == obj.end()) {
     353            0 :         if (optionalProp) {
     354            0 :             HCCL_WARNING("json object has no property called %s", propName);
     355              :         } else {
     356            0 :             HCCL_ERROR("json object has no property called %s", propName);
     357              :         }
     358            0 :         return HCCL_E_NOT_FOUND;
     359              :     }
     360          387 :     propValue = obj[propName];
     361          385 :     CHK_PRT_RET(propValue.size() == 0, HCCL_ERROR("[Get][JsonProperty]get property[%s] size is zero", propName),
     362              :         HCCL_E_PARA);
     363          385 :     return HCCL_SUCCESS;
     364              : }
     365              : 
     366         6102 : HcclResult TopoInfoRanktableParser::GetJsonArrayMemberProperty(const nlohmann::json &obj, const u32 index,
     367              :     const char *propName, std::string &propValue, bool optionalProp) const
     368              : {
     369         6102 :     if (!obj.is_array() || index >= obj.size()) {
     370            2 :         HCCL_ERROR("[Get][JsonArrayMemberProperty]errNo[0x%016llx] index[%u] is out of json object range",
     371              :             HCOM_ERROR_CODE(HCCL_E_NOT_FOUND), index);
     372            2 :         return HCCL_E_PARA;
     373              :     }
     374              : 
     375         6098 :     nlohmann::json subObj = obj.at(index);
     376         6071 :     if (subObj.find(propName) == subObj.end()) {
     377            2 :         if (optionalProp) {
     378            2 :             HCCL_WARNING("json object index[%u] has no property called %s", index, propName);
     379              :         } else {
     380            0 :             RPT_INPUT_ERR(true,
     381              :                 "EI0017",
     382              :                 std::vector<std::string>({"config"}),
     383              :                 std::vector<std::string>({std::string(propName)}));
     384            0 :             HCCL_ERROR("json object index[%u] has no property called %s", index, propName);
     385              :         }
     386            2 :         return HCCL_E_NOT_FOUND;
     387              :     }
     388         6058 :     if (subObj[propName].is_string()) {
     389         6093 :         propValue = subObj[propName];
     390         6093 :         return HCCL_SUCCESS;
     391              :     } else {
     392            0 :         RPT_INPUT_ERR(true,
     393              :             "EI0017",
     394              :             std::vector<std::string>({"config"}),
     395              :             std::vector<std::string>({std::string(propName)}));
     396            0 :         HCCL_ERROR(
     397              :             "[%s][%s]errNo[0x%016llx] json object property value of Name[%s] is not string!",
     398              :             LOG_KEYWORDS_INIT_GROUP.c_str(), LOG_KEYWORDS_RANKTABLE_CHECK.c_str(), HCOM_ERROR_CODE(HCCL_E_PARA),
     399              :             propName);
     400            0 :         return HCCL_E_PARA;
     401              :     }
     402         6095 : }
     403              : 
     404            0 : HcclResult TopoInfoRanktableParser::GetJsonArrayMemberProperty(const nlohmann::json &obj, const u32 index,
     405              :     const char *propName, u32 &propValue, bool optionalProp)
     406              : {
     407            0 :     if (!obj.is_array() || index >= obj.size()) {
     408            0 :         HCCL_ERROR("[Get][JsonArrayMemberProperty]errNo[0x%016llx] index[%u] is out of json object range",
     409              :             HCOM_ERROR_CODE(HCCL_E_NOT_FOUND), index);
     410            0 :         return HCCL_E_PARA;
     411              :     }
     412              : 
     413            0 :     nlohmann::json subObj = obj.at(index);
     414            0 :     if (subObj.find(propName) == subObj.end()) {
     415            0 :         if (optionalProp) {
     416            0 :             HCCL_WARNING("json object index[%u] has no property called %s", index, propName);
     417              :         } else {
     418            0 :             HCCL_ERROR("json object index[%u] has no property called %s", index, propName);
     419              :         }
     420            0 :         return HCCL_E_NOT_FOUND;
     421              :     }
     422            0 :     if (subObj[propName].is_number_unsigned()) {
     423            0 :         propValue = subObj[propName];
     424            0 :         return HCCL_SUCCESS;
     425              :     } else {
     426            0 :         HCCL_ERROR("[Get][JsonArrayMemberProperty]errNo[0x%016llx] json object property value of Name[%s] is "\
     427              :             "not unsigned int!", HCOM_ERROR_CODE(HCCL_E_PARA), propName);
     428            0 :         return HCCL_E_PARA;
     429              :     }
     430            0 : }
     431              : 
     432         1524 : HcclResult TopoInfoRanktableParser::GetJsonArrayMemberProperty(const nlohmann::json &obj, const u32 index,
     433              :     const char *propName, nlohmann::json &propValue, bool optionalProp) const
     434              : {
     435         1524 :     if (!obj.is_array() || index >= obj.size()) {
     436            0 :         HCCL_ERROR("[Get][JsonArrayMemberProperty]errNo[0x%016llx] index[%u] is out of json object range",
     437              :             HCOM_ERROR_CODE(HCCL_E_NOT_FOUND), index);
     438            0 :         return HCCL_E_PARA;
     439              :     }
     440              : 
     441         1523 :     nlohmann::json subObj = obj.at(index);
     442         1514 :     if (subObj.find(propName) == subObj.end()) {
     443            0 :         if (optionalProp) {
     444            0 :             HCCL_WARNING("json object index[%u] has no property called %s", index, propName);
     445              :         } else {
     446            0 :             HCCL_ERROR("json object index[%u] has no property called %s", index, propName);
     447              :         }
     448            0 :         return HCCL_E_NOT_FOUND;
     449              :     }
     450         1513 :     propValue = subObj[propName];
     451         1517 :     CHK_PRT_RET(propValue.size() == 0, HCCL_ERROR("[Get][JsonArrayMemberProperty]get index[%u] property[%s] size is "\
     452              :         "zero", index, propName), HCCL_E_PARA);
     453         1517 :     return HCCL_SUCCESS;
     454         1517 : }
     455              : 
     456              : /* 依据检查类型进行入参内容检查,并将检查选项转为strType带出以便后续信息打印 */
     457         1526 : HcclResult TopoInfoRanktableParser::CheckUniquePara(const JsonUniqueInfoType &type, const std::string &value,
     458              :     string &strType) const
     459              : {
     460         1526 :     auto it = JsonInfoTypeNameMap.find(type);
     461         1523 :     CHK_PRT_RET(it == JsonInfoTypeNameMap.end(),
     462              :         HCCL_ERROR("[Get][JsonArrayMemberProperty][Check][UniquePara]errNo[0x%016llx] jsonUniqueInfoType[%d] "\
     463              :         " is not in JsonInfoTypeNameMap", HCOM_ERROR_CODE(HCCL_E_PARA), type), HCCL_E_PARA);
     464         1523 :     strType = it->second;
     465         1526 :     switch (type) {
     466         1139 :         case JsonUniqueInfoType::UNIQUE_INFO_TYPE_DEVICE_IP:
     467              :         case JsonUniqueInfoType::UNIQUE_INFO_TYPE_BACKUP_DEVICE_IP:
     468              :         case JsonUniqueInfoType::UNIQUE_INFO_TYPE_ETH_IP: {
     469              :             /* 必须是一个有效的ip地址 */
     470         1139 :             HcclIpAddress ip(value);
     471         1139 :             if (ip.IsInvalid()) {
     472            0 :                 RPT_INPUT_ERR(true, "EI0014", std::vector<std::string>({ "value", "variable" ,"expect" }),
     473              :                     std::vector<std::string>({ value, "IP", "a valid IP address" }));
     474            0 :                 HCCL_ERROR("[%s][%s]errNo[0x%016llx] ip[%s] is not a valid ip address", LOG_KEYWORDS_INIT_GROUP.c_str(),
     475              :                     LOG_KEYWORDS_RANKTABLE_CHECK.c_str(), HCOM_ERROR_CODE(HCCL_E_PARA), value.c_str());
     476            0 :                 return HCCL_E_PARA;
     477              :             }
     478         1139 :         }
     479         1139 :             break;
     480              : 
     481          387 :         case JsonUniqueInfoType::UNIQUE_INFO_TYPE_SERVER_ID:
     482              :         case JsonUniqueInfoType::UNIQUE_INFO_TYPE_GROUP_NAME:
     483              :         case JsonUniqueInfoType::UNIQUE_INFO_TYPE_SUPER_POD_ID:
     484              :         case JsonUniqueInfoType::UNIQUE_INFO_TYPE_POD_NAME: {
     485          387 :             if (value.length() > GROUP_NAME_MAX_LEN) {
     486            0 :                 HCCL_ERROR("[Check][UniquePara]errNo[0x%016llx] Name[%s] length[%lu] is too long",
     487              :                     HCOM_ERROR_CODE(HCCL_E_PARA), value.c_str(), value.length());
     488            0 :                 return HCCL_E_PARA;
     489              :             }
     490              :         }
     491          387 :             break;
     492              : 
     493            0 :         case JsonUniqueInfoType::UNIQUE_INFO_TYPE_ETH_NAME:
     494            0 :             break;
     495              : 
     496            0 :         default: {
     497            0 :             HCCL_ERROR("[Check][UniquePara]errNo[0x%016llx] invalid unique info type[%d]",
     498              :                 HCOM_ERROR_CODE(HCCL_E_PARA), type);
     499            0 :             return HCCL_E_PARA;
     500              :         }
     501              :             break;
     502              :     }
     503         1526 :     return HCCL_SUCCESS;
     504            0 : }
     505              : 
     506         1526 : HcclResult TopoInfoRanktableParser::CheckUniqueAndInsertPool(const JsonUniqueInfoType &type,
     507              :     const std::string &value, const JsonCheckOpType &opType)
     508              : {
     509         1526 :     string strUniqueInfoType;
     510              :     /* 先依据type检查入参内容合法 */
     511         1526 :     CHK_RET(CheckUniquePara(type, value, strUniqueInfoType));
     512              :     /* 关键信息添加保存 */
     513         1526 :     auto srvIt = uniqueInfoCheckPool_[static_cast<u32>(type)].find(value);
     514         1524 :     if (opType == JsonCheckOpType::CHECK_OP_TYPE_INSERT) {
     515              :         /* JsonCheckOpType::CHECK_OP_TYPE_INSERT操作插入保存到资源池中,若资源池中存在则报错 */
     516         1524 :         if (srvIt != uniqueInfoCheckPool_[static_cast<u32>(type)].end()) {
     517           14 :             RPT_INPUT_ERR(true, "EI0014", std::vector<std::string>({ "value", "variable" ,"expect" }),
     518              :                 std::vector<std::string>({ value, " \"IP\" is repeate", "please check the ranktable" }));
     519            1 :             HCCL_ERROR("[%s][%s]errNo[0x%016llx] [%s]:[%s] is already exist", LOG_KEYWORDS_INIT_GROUP.c_str(),
     520              :                 LOG_KEYWORDS_RANKTABLE_CHECK.c_str(), HCOM_ERROR_CODE(HCCL_E_PARA), strUniqueInfoType.c_str(),
     521              :                 value.c_str());
     522            1 :             return HCCL_E_PARA;
     523              :         }
     524         1523 :         uniqueInfoCheckPool_[static_cast<u32>(type)].insert(value);
     525            0 :     } else if (opType == JsonCheckOpType::CHECK_OP_TYPE_FIND) {
     526              :         /* JsonCheckOpType::CHECK_OP_TYPE_FIND操作仅在资源池中查找是否存在,不存在报错 */
     527            0 :         if (srvIt == uniqueInfoCheckPool_[static_cast<u32>(type)].end()) {
     528            0 :             HCCL_ERROR("[Check][UniqueAndInsertPool]errNo[0x%016llx] [%s]:[%s] is not exist",
     529              :                 HCOM_ERROR_CODE(HCCL_E_PARA), strUniqueInfoType.c_str(), value.c_str());
     530            0 :             return HCCL_E_NOT_FOUND;
     531              :         }
     532              :     } else {
     533            0 :         HCCL_ERROR("[Check][UniqueAndInsertPool]errNo[0x%016llx] invalid json check op Type[%d]",
     534              :             HCOM_ERROR_CODE(HCCL_E_PARA), opType);
     535            0 :         return HCCL_E_PARA;
     536              :     }
     537         1525 :     return HCCL_SUCCESS;
     538         1528 : }
     539              : 
     540         1137 : void TopoInfoRanktableParser::GenerateServerIdx(const std::string &serverId, u32 &serverIdx)
     541              : {
     542         1137 :     auto it = find(ServerIdRecord_.begin(), ServerIdRecord_.end(), serverId);
     543         1136 :     if (it == ServerIdRecord_.end()) {
     544          400 :         serverIdx = ServerIdRecord_.size();
     545          400 :         ServerIdRecord_.push_back(serverId);
     546              :     } else {
     547         1472 :         serverIdx = std::distance(ServerIdRecord_.begin(), it);
     548              :     }
     549         1136 : }
     550              : 
     551            0 : void TopoInfoRanktableParser::GenerateSuperPodIdx(const std::string &superPodId, u32 &superPodIdx)
     552              : {
     553            0 :     auto it = find(superPodRecord_.begin(), superPodRecord_.end(), superPodId);
     554            0 :     if (it == superPodRecord_.end()) {
     555            0 :         superPodIdx = superPodRecord_.size();
     556            0 :         superPodRecord_.push_back(superPodId);
     557              :     } else {
     558            0 :         superPodIdx = std::distance(superPodRecord_.begin(), it);
     559              :     }
     560            0 :     HCCL_INFO("GenerateSuperPodIdx superPodId[%s], superPodIdx[%u]", superPodId.c_str(), superPodIdx);
     561            0 : }
     562              : 
     563         1136 : HcclResult TopoInfoRanktableParser::ConvertIpAddress(const std::string &ipStr, HcclIpAddress &ipAddr)
     564              : {
     565         1136 :     HcclResult ret = ipAddr.SetReadableAddress(ipStr);
     566         1138 :     RPT_INPUT_ERR(ret != HCCL_SUCCESS, "EI0014", std::vector<std::string>({ "value", "variable" ,"expect" }),
     567              :         std::vector<std::string>({ ipStr, "IP", "a valid IP address" }));
     568         1138 :     CHK_PRT_RET(ret != HCCL_SUCCESS, HCCL_ERROR("[%s][%s]ipStr[%s] convert failed",
     569              :         LOG_KEYWORDS_INIT_GROUP.c_str(), LOG_KEYWORDS_RANKTABLE_CHECK.c_str(),ipStr.c_str()), ret);
     570         1138 :     return ret;
     571            0 : }
        

Generated by: LCOV version 2.0-1