2 Copyright (c) 2018-2022 Nokia.
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
8 http://www.apache.org/licenses/LICENSE-2.0
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
18 * This source code is part of the near-RT RIC (RAN Intelligent Controller)
19 * platform project (RICP).
22 #include "private/databaseconfigurationimpl.hpp"
23 #include <arpa/inet.h>
24 #include <boost/crc.hpp>
25 #include <boost/lexical_cast.hpp>
26 #include <boost/algorithm/string.hpp>
28 using namespace shareddatalayer;
32 const std::string& getDefaultHost()
34 static const std::string defaultHost("localhost");
38 const uint16_t DEFAULT_PORT(6379U);
39 const uint16_t DEFAULT_SENTINEL_PORT(26379U);
40 const std::string DEFAULT_SENTINEL_MASTER_GROUP_NAME("dbaasmaster");
43 DatabaseConfigurationImpl::DatabaseConfigurationImpl():
44 dbType(DbType::UNKNOWN)
48 DatabaseConfigurationImpl::~DatabaseConfigurationImpl()
52 void DatabaseConfigurationImpl::checkAndApplyDbType(const std::string& type)
54 if (type == "redis-standalone")
55 dbType = DatabaseConfiguration::DbType::REDIS_STANDALONE;
56 else if (type == "redis-cluster")
57 dbType = DatabaseConfiguration::DbType::REDIS_CLUSTER;
58 else if (type == "redis-sentinel")
59 dbType = DatabaseConfiguration::DbType::REDIS_SENTINEL;
60 else if (type == "sdl-standalone-cluster")
61 dbType = DatabaseConfiguration::DbType::SDL_STANDALONE_CLUSTER;
62 else if (type == "sdl-sentinel-cluster")
63 dbType = DatabaseConfiguration::DbType::SDL_SENTINEL_CLUSTER;
65 throw DatabaseConfiguration::InvalidDbType(type);
68 DatabaseConfiguration::DbType DatabaseConfigurationImpl::getDbType() const
73 void DatabaseConfigurationImpl::checkAndApplyServerAddress(const std::string& address)
75 serverAddresses.push_back(HostAndPort(address, htons(DEFAULT_PORT)));
78 bool DatabaseConfigurationImpl::isEmpty() const
80 return serverAddresses.empty();
83 DatabaseConfiguration::Addresses DatabaseConfigurationImpl::getServerAddresses() const
85 return serverAddresses;
88 DatabaseConfiguration::Addresses DatabaseConfigurationImpl::getServerAddresses(const boost::optional<std::size_t>& addressIndex) const
91 return { HostAndPort(serverAddresses.at(*addressIndex)) };
93 return serverAddresses;
96 DatabaseConfiguration::Addresses DatabaseConfigurationImpl::getDefaultServerAddresses() const
98 return { HostAndPort(getDefaultHost(), htons(DEFAULT_PORT)) };
101 void DatabaseConfigurationImpl::checkAndApplySentinelPorts(const std::string& portsEnvStr)
103 std::vector<std::string> ports;
104 boost::split(ports, portsEnvStr, boost::is_any_of(","));
106 for (auto port : ports)
109 sentinelPorts.push_back(htons(boost::lexical_cast<uint16_t>(port)));
111 catch (boost::bad_lexical_cast const &) {
117 boost::optional<HostAndPort> DatabaseConfigurationImpl::getSentinelAddress(const boost::optional<std::size_t>& addressIndex) const
119 std::size_t index(addressIndex ? *addressIndex : 0);
120 uint16_t port((sentinelPorts.size() > 0 && index < sentinelPorts.size()) ? sentinelPorts.at(index) : htons(DEFAULT_SENTINEL_PORT));
122 if (!(serverAddresses.size() > 0))
125 return { HostAndPort(serverAddresses.at(index).getHost(), port) };
128 void DatabaseConfigurationImpl::checkAndApplySentinelMasterNames(const std::string& sentinelMasterNamesEnvStr)
130 boost::split(sentinelMasterNames, sentinelMasterNamesEnvStr, boost::is_any_of(","));
133 std::string DatabaseConfigurationImpl::getSentinelMasterName(const boost::optional<std::size_t>& addressIndex) const
135 std::size_t index(addressIndex ? *addressIndex : 0);
136 return ((sentinelMasterNames.size() > 0 && index < sentinelMasterNames.size()) ? sentinelMasterNames.at(index) : DEFAULT_SENTINEL_MASTER_GROUP_NAME);