void SonicDBConfig::initializeGlobalConfig()

in common/dbconnector.cpp [78:163]


void SonicDBConfig::initializeGlobalConfig(const string &file)
{
    std::string dir_name;
    std::lock_guard<std::recursive_mutex> guard(m_db_info_mutex);

    SWSS_LOG_ENTER();

    if (m_global_init)
    {
        SWSS_LOG_ERROR("SonicDBConfig Global config is already initialized");
        return;
    }

    ifstream i(file);
    if (i.good())
    {
        // Get the directory name from the file path given as input.
        std::string::size_type pos = file.rfind("/");
        if( pos != std::string::npos)
        {
            dir_name = file.substr(0,pos+1);
        }

        try
        {
            json j;
            i >> j;

            for (auto& element : j["INCLUDES"])
            {
                std::unordered_map<std::string, SonicDBInfo> db_entry;
                std::map<std::string, RedisInstInfo> inst_entry;
                std::unordered_map<int, std::string> separator_entry;
                std::string local_file = dir_name;
                local_file.append(element["include"]);
                SonicDBKey key;
                if(!element["namespace"].empty())
                {
                    key.netns = element["namespace"];
                }

                if (!element["container_name"].empty())
                {
                    key.containerName = element["container_name"];
                }

                // If database_config.json is already initlized via SonicDBConfig::initialize
                // skip initializing it here again.
                if (key.isEmpty() && m_init)
                {
                    continue;
                }

                parseDatabaseConfig(local_file, inst_entry, db_entry, separator_entry);
                m_inst_info[key] = inst_entry;
                m_db_info[key] = db_entry;
                m_db_separator[key] = separator_entry;

                if(key.isEmpty())
                {
                    // Make regular init also done
                    m_init = true;
                }
            }
        }

        catch (domain_error& e)
        {
            SWSS_LOG_ERROR("key doesn't exist in json object, NULL value has no iterator >> %s\n", e.what());
            throw runtime_error("key doesn't exist in json object, NULL value has no iterator >> " + string(e.what()));
        }
        catch (exception &e)
        {
            SWSS_LOG_ERROR("Sonic database config file syntax error >> %s\n", e.what());
            throw runtime_error("Sonic database config file syntax error >> " + string(e.what()));
        }
    }
    else
    {
        SWSS_LOG_ERROR("Sonic database config global file doesn't exist at %s\n", file.c_str());
    }


    // Set it as the global config file is already parsed and init done.
    m_global_init = true;
}