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 783 : std::string GetJsonProperty(const nlohmann::json& obj, const char* propName, bool required)
18 : {
19 783 : if (propName == nullptr) {
20 1 : std::string msg = StringFormat("[%s] propName is nullptr", __func__);
21 1 : THROW<NullPtrException>(msg);
22 1 : }
23 782 : if (!required) {
24 28 : return obj.value<std::string>(propName, "");
25 : }
26 :
27 768 : if (!obj.contains(propName)) {
28 8 : RPT_INPUT_ERR(
29 : true, "EI0017", std::vector<std::string>({"config"}), std::vector<std::string>({std::string(propName)}));
30 1 : THROW<InvalidParamsException>(
31 3 : StringFormat("[Get][JsonProperty] json object does not contain property[%s].", propName));
32 : }
33 767 : std::string value = obj.at(propName).get<std::string>();
34 767 : return value;
35 769 : }
36 :
37 500 : u32 GetJsonPropertyUInt(const nlohmann::json& obj, const char* propName, bool required, u32 defaultValue)
38 : {
39 500 : if (!required) {
40 84 : return obj.value<u32>(propName, defaultValue);
41 : }
42 :
43 416 : if (!obj.contains(propName)) {
44 8 : RPT_INPUT_ERR(
45 : true, "EI0017", std::vector<std::string>({"config"}), std::vector<std::string>({std::string(propName)}));
46 1 : THROW<InvalidParamsException>(
47 3 : StringFormat("[Get][JsonPropertyUInt] json object does not contain property[%s].", propName));
48 : }
49 :
50 415 : s64 value = obj.at(propName).get<s64>();
51 415 : if (value < 0 || value > UINT32_MAX) {
52 14 : 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 1 : 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 414 : return static_cast<u32>(value);
61 4 : }
62 :
63 2 : s32 GetJsonPropertySInt(const nlohmann::json& obj, const char* propName, bool required, s32 defaultValue)
64 : {
65 2 : if (!required) {
66 0 : return obj.value<s32>(propName, defaultValue);
67 : }
68 :
69 2 : if (!obj.contains(propName)) {
70 8 : RPT_INPUT_ERR(
71 : true, "EI0017", std::vector<std::string>({"config"}), std::vector<std::string>({std::string(propName)}));
72 1 : THROW<InvalidParamsException>(
73 3 : StringFormat("[Get][JsonPropertySInt] json object does not contain property[%s].", propName));
74 : }
75 :
76 1 : s64 value = obj.at(propName).get<s64>();
77 1 : if (value < INT_MIN || value > INT_MAX) {
78 14 : 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 2 : 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 4 : }
90 :
91 521 : void GetJsonPropertyList(const nlohmann::json& obj, const char* propName, nlohmann::json& listObj)
92 : {
93 521 : if (!obj.contains(propName)) {
94 8 : RPT_INPUT_ERR(
95 : true, "EI0017", std::vector<std::string>({"config"}), std::vector<std::string>({std::string(propName)}));
96 1 : THROW<InvalidParamsException>(
97 3 : StringFormat("[Get][JsonPropertyList] json object does not contain property[%s].", propName));
98 : }
99 :
100 520 : listObj = obj.at(propName);
101 520 : if (!listObj.is_array()) {
102 14 : 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 1 : 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 523 : }
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
|