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 <sstream>
12 : #include <vector>
13 : #include <string>
14 : #include <unordered_map>
15 : #include "json_parser.h"
16 : #include "invalid_params_exception.h"
17 : #include "exception_util.h"
18 : #include "control_plane.h"
19 : #include "topo_common_types.h"
20 : namespace Hccl {
21 : using namespace std;
22 :
23 : const unordered_map<string, AddrType> ControlPlane::strToAddrType
24 : = (unordered_map<string, AddrType>{{"EID", AddrType::EID}, {"IPV4", AddrType::IPV4}, {"IPV6", AddrType::IPV6}});
25 :
26 6 : void ControlPlane::Deserialize(const nlohmann::json& controlPlaneJson)
27 : {
28 6 : string addrTypeStr;
29 6 : std::string msgAddrtype = "error occurs when parser object of propName \"addr_type\"";
30 :
31 6 : TRY_CATCH_THROW(InvalidParamsException, msgAddrtype, addrTypeStr = GetJsonProperty(controlPlaneJson, "addr_type"););
32 :
33 6 : if (!IsStringInAddrType(addrTypeStr)) {
34 2 : THROW<InvalidParamsException>(StringFormat("[ControlPlane::%s] failed with Invalid addrType. ", __func__));
35 : }
36 5 : addrType = strToAddrType.at(addrTypeStr);
37 :
38 5 : std::string address;
39 5 : std::string msgAddr = "error occurs when parser object of propName \"addr\"";
40 5 : const int MAX_DISPLAY_LEN = 128;
41 5 : TRY_CATCH_THROW(InvalidParamsException, msgAddr, address = GetJsonProperty(controlPlaneJson, "addr"););
42 :
43 5 : if (address.length() < MIN_VALUE_ADDR || address.length() > MAX_VALUE_ADDR) {
44 0 : THROW<InvalidParamsException>(StringFormat(
45 : "addr [%.*s] length is out of range [%u] to [%u]", MAX_DISPLAY_LEN, address.c_str(), MIN_VALUE_ADDR,
46 : MAX_VALUE_ADDR));
47 : }
48 :
49 5 : if (addrTypeStr == "IPV4") {
50 4 : IPV4ToAddr(address);
51 2 : } else if (addrTypeStr == "IPV6") {
52 1 : IPV6ToAddr(address);
53 1 : } else if (addrTypeStr == "EID") {
54 1 : EidToAddr(address);
55 : }
56 :
57 4 : std::string msgListenport = "error occurs when parser object of propName \"listen_port\"";
58 4 : TRY_CATCH_THROW(InvalidParamsException, msgListenport,
59 : listenPort = GetJsonPropertyUInt(controlPlaneJson, "listen_port"););
60 :
61 4 : if (listenPort < MIN_VALUE_LISTENPORT || listenPort > MAX_VALUE_LISTENPORT) {
62 2 : THROW<InvalidParamsException>(StringFormat(
63 : "listen_port [%u] length is out of range [%u] to [%u]", listenPort, MIN_VALUE_LISTENPORT,
64 : MAX_VALUE_LISTENPORT));
65 : }
66 14 : }
67 :
68 3 : void ControlPlane::EidToAddr(std::string address)
69 : {
70 3 : if (address.length() != URMA_EID_LEN * URMA_EID_NUM_TWO) {
71 1 : THROW<InvalidParamsException>(
72 3 : StringFormat("[ControlPlane::%s] failed with rankAddrs : error in length. ", __func__));
73 2 : } else if (!IpAddress::IsEID(address)) {
74 1 : THROW<InvalidParamsException>(
75 3 : StringFormat("[ControlPlane::%s] failed with rankAddrs : error in format. ", __func__));
76 : }
77 1 : Eid eid = IpAddress::StrToEID(address);
78 1 : IpAddress ipAddress0(eid);
79 1 : addr = ipAddress0;
80 1 : }
81 :
82 3 : void ControlPlane::IPV4ToAddr(std::string address)
83 : {
84 3 : s32 ipFamily = AF_INET;
85 :
86 3 : if (IpAddress::IsIPv4(address)) {
87 2 : ipFamily = AF_INET;
88 : } else {
89 2 : THROW<InvalidParamsException>(StringFormat("[ControlPlane::%s] failed with addrs is error. ", __func__));
90 : }
91 2 : IpAddress ipAddress0(address, ipFamily);
92 2 : addr = ipAddress0;
93 2 : }
94 :
95 1 : void ControlPlane::IPV6ToAddr(std::string address)
96 : {
97 1 : s32 ipFamily = AF_INET6;
98 :
99 1 : if (IpAddress::IsIPv6(address)) {
100 1 : ipFamily = AF_INET6;
101 : } else {
102 0 : THROW<InvalidParamsException>(StringFormat("[ControlPlane::%s] failed with addr is error. ", __func__));
103 : }
104 1 : IpAddress ipAddress0(address, ipFamily);
105 1 : addr = ipAddress0;
106 1 : }
107 :
108 3 : std::string ControlPlane::Describe() const
109 : {
110 : return StringFormat(
111 6 : "ControlPlane[addrType=%s, addr=%s, listenPort=%u]", addrType.Describe().c_str(), addr.Describe().c_str(),
112 9 : listenPort);
113 : }
114 :
115 32 : void ControlPlane::GetBinStream(BinaryStream& binStream) const
116 : {
117 32 : binStream << static_cast<u32>(addrType) << listenPort;
118 32 : addr.GetBinStream(binStream);
119 32 : }
120 :
121 9 : ControlPlane::ControlPlane(BinaryStream& binStream)
122 : {
123 9 : u32 addrTypeInt{0};
124 9 : binStream >> addrTypeInt;
125 9 : addrType = static_cast<AddrType::Value>(addrTypeInt);
126 9 : binStream >> listenPort;
127 9 : IpAddress address(binStream);
128 9 : addr = address;
129 9 : }
130 :
131 : } // namespace Hccl
|