X-Git-Url: https://gerrit.o-ran-sc.org/r/gitweb?a=blobdiff_plain;f=src%2Fdatabaseconfigurationimpl.cpp;h=ac691ab4e77ef30227d9d4206b1ca772972c7d52;hb=refs%2Fchanges%2F57%2F11757%2F3;hp=a8f64e6cb514197d51641d15b287cb4f6cd4b0c4;hpb=a0745d294dcd72f7d78ea4c3accd3b477dd668a5;p=ric-plt%2Fsdl.git diff --git a/src/databaseconfigurationimpl.cpp b/src/databaseconfigurationimpl.cpp index a8f64e6..ac691ab 100644 --- a/src/databaseconfigurationimpl.cpp +++ b/src/databaseconfigurationimpl.cpp @@ -1,5 +1,5 @@ /* - Copyright (c) 2018-2019 Nokia. + Copyright (c) 2018-2022 Nokia. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. @@ -21,6 +21,9 @@ #include "private/databaseconfigurationimpl.hpp" #include +#include +#include +#include using namespace shareddatalayer; @@ -34,6 +37,7 @@ namespace const uint16_t DEFAULT_PORT(6379U); const uint16_t DEFAULT_SENTINEL_PORT(26379U); + const std::string DEFAULT_SENTINEL_MASTER_GROUP_NAME("dbaasmaster"); } DatabaseConfigurationImpl::DatabaseConfigurationImpl(): @@ -53,6 +57,10 @@ void DatabaseConfigurationImpl::checkAndApplyDbType(const std::string& type) dbType = DatabaseConfiguration::DbType::REDIS_CLUSTER; else if (type == "redis-sentinel") dbType = DatabaseConfiguration::DbType::REDIS_SENTINEL; + else if (type == "sdl-standalone-cluster") + dbType = DatabaseConfiguration::DbType::SDL_STANDALONE_CLUSTER; + else if (type == "sdl-sentinel-cluster") + dbType = DatabaseConfiguration::DbType::SDL_SENTINEL_CLUSTER; else throw DatabaseConfiguration::InvalidDbType(type); } @@ -77,27 +85,53 @@ DatabaseConfiguration::Addresses DatabaseConfigurationImpl::getServerAddresses() return serverAddresses; } +DatabaseConfiguration::Addresses DatabaseConfigurationImpl::getServerAddresses(const boost::optional& addressIndex) const +{ + if (addressIndex) + return { HostAndPort(serverAddresses.at(*addressIndex)) }; + + return serverAddresses; +} + DatabaseConfiguration::Addresses DatabaseConfigurationImpl::getDefaultServerAddresses() const { return { HostAndPort(getDefaultHost(), htons(DEFAULT_PORT)) }; } -void DatabaseConfigurationImpl::checkAndApplySentinelAddress(const std::string& address) +void DatabaseConfigurationImpl::checkAndApplySentinelPorts(const std::string& portsEnvStr) { - sentinelAddress = HostAndPort(address, htons(DEFAULT_SENTINEL_PORT)); + std::vector ports; + boost::split(ports, portsEnvStr, boost::is_any_of(",")); + + for (auto port : ports) + { + try { + sentinelPorts.push_back(htons(boost::lexical_cast(port))); + } + catch (boost::bad_lexical_cast const &) { + continue; + } + } } -boost::optional DatabaseConfigurationImpl::getSentinelAddress() const +boost::optional DatabaseConfigurationImpl::getSentinelAddress(const boost::optional& addressIndex) const { - return sentinelAddress; + std::size_t index(addressIndex ? *addressIndex : 0); + uint16_t port((sentinelPorts.size() > 0 && index < sentinelPorts.size()) ? sentinelPorts.at(index) : htons(DEFAULT_SENTINEL_PORT)); + + if (!(serverAddresses.size() > 0)) + return {}; + + return { HostAndPort(serverAddresses.at(index).getHost(), port) }; } -void DatabaseConfigurationImpl::checkAndApplySentinelMasterName(const std::string& name) +void DatabaseConfigurationImpl::checkAndApplySentinelMasterNames(const std::string& sentinelMasterNamesEnvStr) { - sentinelMasterName = name; + boost::split(sentinelMasterNames, sentinelMasterNamesEnvStr, boost::is_any_of(",")); } -std::string DatabaseConfigurationImpl::getSentinelMasterName() const +std::string DatabaseConfigurationImpl::getSentinelMasterName(const boost::optional& addressIndex) const { - return sentinelMasterName; + std::size_t index(addressIndex ? *addressIndex : 0); + return ((sentinelMasterNames.size() > 0 && index < sentinelMasterNames.size()) ? sentinelMasterNames.at(index) : DEFAULT_SENTINEL_MASTER_GROUP_NAME); }