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 : #include <climits>
11 : #include "json_parser.h"
12 : #include "adapter_error_manager_pub.h"
13 :
14 : namespace Hccl {
15 :
16 483 : std::string GetJsonProperty(const nlohmann::json &obj, const char *propName, bool required)
17 : {
18 483 : if (propName == nullptr) {
19 0 : std::string msg = StringFormat("[%s] propName is nullptr", __func__);
20 0 : THROW<NullPtrException>(msg);
21 0 : }
22 483 : if (!required) {
23 166 : return obj.value<std::string>(propName, "");
24 : }
25 :
26 400 : if (!obj.contains(propName)) {
27 16 : RPT_INPUT_ERR(true, "EI0017", std::vector<std::string>({"config"}),
28 : std::vector<std::string>({std::string(propName)}));
29 4 : THROW<InvalidParamsException>(StringFormat("[Get][JsonProperty] json object does not contain property[%s].",
30 : propName));
31 : }
32 398 : std::string value = obj.at(propName).get<std::string>();
33 398 : return value;
34 402 : }
35 :
36 529 : u32 GetJsonPropertyUInt(const nlohmann::json &obj, const char *propName, bool required, u32 defaultValue)
37 : {
38 529 : if (!required) {
39 36 : return obj.value<u32>(propName, defaultValue);
40 : }
41 :
42 493 : if (!obj.contains(propName)) {
43 40 : RPT_INPUT_ERR(true, "EI0017", std::vector<std::string>({"config"}),
44 : std::vector<std::string>({std::string(propName)}));
45 10 : THROW<InvalidParamsException>(StringFormat("[Get][JsonPropertyUInt] json object does not contain property[%s].",
46 : propName));
47 : }
48 :
49 488 : s64 value = obj.at(propName).get<s64>();
50 487 : if (value < 0 || value > UINT32_MAX) {
51 28 : RPT_INPUT_ERR(true, "EI0014", std::vector<std::string>({ "value", "variable" ,"expect" }),
52 : std::vector<std::string>({std::to_string(value), std::string(propName), "0 ~ UINT32_MAX"}));
53 2 : THROW<InvalidParamsException>(StringFormat("[Get][JsonPropertyUInt]errNo[0x%016llx]:json object "
54 : "property value of Name[%s] should be an unsigned 32-bit integer but acutally not!",
55 : HCOM_ERROR_CODE(HcclResult::HCCL_E_PARA), propName));
56 : }
57 485 : return static_cast<u32>(value);
58 14 : }
59 :
60 0 : s32 GetJsonPropertySInt(const nlohmann::json &obj, const char *propName, bool required, s32 defaultValue)
61 : {
62 0 : if (!required) {
63 0 : return obj.value<s32>(propName, defaultValue);
64 : }
65 :
66 0 : if (!obj.contains(propName)) {
67 0 : RPT_INPUT_ERR(true, "EI0017", std::vector<std::string>({"config"}),
68 : std::vector<std::string>({std::string(propName)}));
69 0 : THROW<InvalidParamsException>(StringFormat("[Get][JsonPropertySInt] json object does not contain property[%s].",
70 : propName));
71 : }
72 :
73 0 : s64 value = obj.at(propName).get<s64>();
74 0 : if (value < INT_MIN || value > INT_MAX) {
75 0 : RPT_INPUT_ERR(true, "EI0014", std::vector<std::string>({ "value", "variable" ,"expect" }),
76 : std::vector<std::string>({std::to_string(value), std::string(propName), "0 ~ INT_MAX"}));
77 0 : THROW<InvalidParamsException>(StringFormat("[Get][JsonPropertySInt]errNo[0x%016llx]:json object "
78 : "property value of Name[%s] is not signed number!",
79 : HCOM_ERROR_CODE(HcclResult::HCCL_E_PARA), propName));
80 : }
81 0 : HCCL_INFO("[Get][JsonPropertySInt]json object property value of Name [%s] is [%d]", propName,
82 : static_cast<s32>(value));
83 0 : return static_cast<s32>(value);
84 0 : }
85 :
86 311 : void GetJsonPropertyList(const nlohmann::json &obj, const char *propName, nlohmann::json &listObj)
87 : {
88 311 : if (!obj.contains(propName)) {
89 0 : RPT_INPUT_ERR(true, "EI0017", std::vector<std::string>({"config"}),
90 : std::vector<std::string>({std::string(propName)}));
91 0 : THROW<InvalidParamsException>(StringFormat("[Get][JsonPropertyList] json object does not contain property[%s].",
92 : propName));
93 : }
94 :
95 311 : listObj = obj.at(propName);
96 311 : if (!listObj.is_array()) {
97 0 : RPT_INPUT_ERR(true, "EI0014", std::vector<std::string>({ "value", "variable" ,"expect" }),
98 : std::vector<std::string>({std::string(listObj.type_name()), std::string(propName), "array"}));
99 0 : THROW<InvalidParamsException>(StringFormat("[Get][GetJsonPropertyList]errNo[0x%016llx]:json object "
100 : "property value of Name \"%s\" is not list!",
101 : HCOM_ERROR_CODE(HcclResult::HCCL_E_PARA), propName));
102 : }
103 311 : }
104 :
105 :
106 0 : void JsonParser::ParseFileToJson(const std::string &filePath, nlohmann::json &parseInformation) const
107 : {
108 : // 校验文件是否存在
109 0 : char resolvedPath[PATH_MAX] = {0};
110 0 : if (realpath(filePath.c_str(), resolvedPath) == nullptr) {
111 0 : RPT_INPUT_ERR(true, "EI0004", std::vector<std::string>({"error_reason", "ranktable_path"}),
112 : std::vector<std::string>({filePath, "The rankTable file path does not exist, the permission is insufficient, or the JSON format is incorrect."}));
113 0 : THROW<InvalidParamsException>(
114 0 : StringFormat("[Get][RanktableRealPath]errNo[0x%016llx] path %s is not a valid real path",
115 : HCOM_ERROR_CODE(HcclResult::HCCL_E_PARA), filePath.c_str()));
116 : }
117 :
118 0 : HCCL_INFO("waiting for json file load complete");
119 0 : std::ifstream infoFile(resolvedPath, std::ifstream::in);
120 0 : if (!infoFile) {
121 0 : THROW<InternalException>(StringFormat("[Read][File]errNo[0x%016llx],open file %s failed",
122 : HCOM_ERROR_CODE(HcclResult::HCCL_E_OPEN_FILE_FAILURE), resolvedPath));
123 : }
124 :
125 0 : ParseInformation(parseInformation, infoFile);
126 0 : infoFile.close();
127 0 : }
128 :
129 : } // namespace Hccl
|