LCOV - code coverage report
Current view: top level - coll_communicator_mgr/rank_graph/rank_table_info - address_info.cc (source / functions) Coverage Total Hit
Test: coverage.info Lines: 93.4 % 91 85
Test Date: 2026-07-28 12:11:00 Functions: 100.0 % 7 7

            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 "address_info.h"
      12              : 
      13              : #include <sstream>
      14              : #include <vector>
      15              : #include <string>
      16              : #include <unordered_map>
      17              : #include "json_parser.h"
      18              : #include "invalid_params_exception.h"
      19              : #include "exception_util.h"
      20              : #include "adapter_error_manager_pub.h"
      21              : 
      22              : namespace Hccl {
      23              : using namespace std;
      24              : const unordered_map<string, AddrType> AddressInfo::strToAddrType
      25              :     = (unordered_map<string, AddrType>{{"EID", AddrType::EID}, {"IPV4", AddrType::IPV4}, {"IPV6", AddrType::IPV6}});
      26              : 
      27          309 : void AddressInfo::Deserialize(const nlohmann::json &addressInfoJson)
      28              : {
      29          309 :     std::string addrTypeStr;
      30          309 :     std::string msgAddrtype = "error occurs when parser object of propName \"addr_type\"";
      31          309 :     TRY_CATCH_THROW(InvalidParamsException, msgAddrtype, addrTypeStr = GetJsonProperty(addressInfoJson, "addr_type"););
      32              :     
      33          309 :     if (!IsStringInAddrType(addrTypeStr)) {
      34            2 :         THROW<InvalidParamsException>(StringFormat("[AddressInfo::%s] failed with Invalid addrType. ",
      35              :                                                    __func__));
      36              :     }
      37          308 :     addrType = strToAddrType.at(addrTypeStr);
      38              :     
      39          308 :     std::string address;
      40          308 :     std::string msgAddr = "error occurs when parser object of propName \"addr\"";
      41          308 :     const int MAX_DISPLAY_LEN = 128;
      42          308 :     TRY_CATCH_THROW(InvalidParamsException, msgAddr, address = GetJsonProperty(addressInfoJson, "addr", false););
      43              :    
      44          308 :     if (address.length() < MIN_VALUE_ADDR_LENGRH || address.length() > MAX_VALUE_ADDR_LENGRH) {
      45            0 :         RPT_INPUT_ERR(true, "EI0014", std::vector<std::string>({"value", "variable", "expect"}), 
      46              :             std::vector<std::string>({address, "addr", "A ip address."}));
      47            0 :         THROW<InvalidParamsException>(StringFormat("addr [%.*s] length is out of range [%u] to [%u]", MAX_DISPLAY_LEN, address.c_str(), MIN_VALUE_ADDR_LENGRH, MAX_VALUE_ADDR_LENGRH));
      48              :     }
      49              : 
      50          784 :     HCCL_INFO("[AddressInfo::%s] addrTypeStr is[%s]", __func__, addrTypeStr.c_str());
      51          308 :     if (addrTypeStr == "IPV4") {
      52          305 :         IPV4ToAddr(address);
      53            7 :     } else if (addrTypeStr == "IPV6") {
      54            7 :         IPV6ToAddr(address);
      55            3 :     } else if (addrTypeStr == "EID"){
      56            5 :         EidToAddr(address);
      57              :     }
      58              : 
      59          299 :     planeId=addressInfoJson.value<std::string>("plane_id", "0");
      60          299 :     if (planeId.length() > MAX_VALUE_PLANEID) {
      61            0 :         THROW<InvalidParamsException>(StringFormat("plane_id [%s] length is out of range [%u] to [%u]", planeId.c_str(),MIN_VALUE_PLANEID, MAX_VALUE_PLANEID));
      62              :     }
      63              : 
      64          299 :     nlohmann::json portsJsons;
      65          299 :     std::string msgPortlist = "error occurs when parser object of propName \"ports\"";
      66          299 :     TRY_CATCH_THROW(InvalidParamsException, msgPortlist, GetJsonPropertyList(addressInfoJson, "ports", portsJsons););
      67          710 :     for (auto &portsJson : portsJsons) {
      68          412 :     if (portsJson.get<std::string>().size() < MIN_VALUE_PORT_LENGTH || portsJson.get<std::string>().size() > MAX_VALUE_PORT_LENGTH){
      69            3 :         THROW<InvalidParamsException>(StringFormat("portsString [%u] length is out of range [%u] to [%u]", portsJson.get<std::string>().size(), MIN_VALUE_PORT_LENGTH, MAX_VALUE_PORT_LENGTH));
      70              :     }
      71          411 :         ports.emplace(portsJson);
      72              :     }
      73          298 :     if (ports.size() < MIN_VALUE_PORT || ports.size() > MAX_VALUE_PORT) {
      74            2 :         THROW<InvalidParamsException>(StringFormat("ports [%u] length is out of range [%u] to [%u]", ports.size(), MIN_VALUE_PORT, MAX_VALUE_PORT));
      75              :     }
      76          347 : }
      77              : 
      78            3 : void AddressInfo::EidToAddr(std::string address)
      79              : {
      80            3 :     if(address.length() != URMA_EID_LEN * URMA_EID_NUM_TWO) {
      81            2 :         THROW<InvalidParamsException>(StringFormat("[AddressInfo::%s] failed with rankAddrs : error in length. ", __func__));
      82            2 :     }else if(!IpAddress::IsEID(address)) {
      83            2 :         THROW<InvalidParamsException>(StringFormat("[AddressInfo::%s] failed with rankAddrs : error in format. ", __func__));
      84              :     }
      85            1 :     Eid eid = IpAddress::StrToEID(address);
      86            1 :     IpAddress ipAddress0(eid);
      87            1 :     addr = ipAddress0;
      88            1 : }
      89              : 
      90          301 : void AddressInfo::IPV4ToAddr(std::string address)
      91              : {
      92          301 :     s32 ipFamily = AF_INET;
      93              : 
      94          301 :         if (IpAddress::IsIPv4(address)) {
      95          297 :             ipFamily = AF_INET;
      96              :         }else {
      97            8 :             THROW<InvalidParamsException>(StringFormat("[AddressInfo::%s] failed with addrs is error. "
      98              :                                                        ,__func__));
      99              :         }
     100          297 :        IpAddress ipAddress0(address, ipFamily);
     101          297 :        addr=ipAddress0;
     102          773 :     HCCL_INFO("[AddressInfo::%s] IpAddress is[%s]", __func__, ipAddress0.Describe().c_str());
     103          297 : }
     104              : 
     105            4 : void AddressInfo::IPV6ToAddr(std::string address)
     106              : {
     107            4 :     s32 ipFamily = AF_INET6;
     108              : 
     109            4 :     if (IpAddress::IsIPv6(address)) {
     110            1 :         ipFamily = AF_INET6;
     111              :     }else {
     112            6 :         THROW<InvalidParamsException>(StringFormat("[AddressInfo::%s] failed with addr is error. "
     113              :                                                     ,__func__));
     114              :     }
     115            1 :     IpAddress ipAddress0(address, ipFamily);
     116            1 :     addr=ipAddress0;
     117            1 : }
     118              : 
     119            1 : std::string AddressInfo::Describe() const
     120              : {
     121              :     return StringFormat("AddressInfo[addrType=%s, addr=%s, planeId=%s, portsize=%u socketPort_=%u]",
     122            2 :                         addrType.Describe().c_str(), addr.Describe().c_str(), planeId.c_str(), ports.size(), socketPort_);
     123              : }
     124              : 
     125           22 : AddressInfo::AddressInfo(BinaryStream &binStream)
     126              : {
     127           11 :     IpAddress address(binStream);
     128           11 :     addr=address;
     129           11 :     u32 addrTypeInt{0};
     130           11 :     binStream >> addrTypeInt;
     131           11 :     addrType = static_cast<AddrType::Value>(addrTypeInt);
     132           11 :     size_t portsSize{0};
     133           11 :     binStream >> portsSize;
     134           34 :     for (u32 i = 0; i < portsSize; i++) {
     135           23 :         string port;
     136           23 :         binStream>>port;
     137           23 :         ports.emplace(port);
     138           23 :     }
     139           11 :     binStream>>planeId;
     140           11 :     binStream>>socketPort_;
     141           11 : }
     142              : 
     143          113 : void AddressInfo::GetBinStream(BinaryStream &binStream) const
     144              : {
     145          113 :     if (ports.size() == 0) {
     146            0 :         std::string msg = StringFormat("ports size is zero.");
     147            0 :         THROW<InvalidParamsException>(msg);
     148            0 :     }
     149          113 :     addr.GetBinStream(binStream);
     150          113 :     binStream << static_cast<u32>(addrType);
     151          113 :     binStream << ports.size();
     152          280 :     for (auto &it : ports) {
     153          167 :         binStream<<it;
     154              :     }
     155          113 :     binStream<<planeId;
     156          113 :     binStream<<socketPort_;
     157          113 : }   
     158              : 
     159              : } // namespace Hccl
        

Generated by: LCOV version 2.0-1