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