2 Copyright (c) 2018-2019 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>
26 using namespace shareddatalayer;
30 const std::string& getDefaultHost()
32 static const std::string defaultHost("localhost");
36 const uint16_t DEFAULT_PORT(6379U);
37 const uint16_t DEFAULT_SENTINEL_PORT(26379U);
40 DatabaseConfigurationImpl::DatabaseConfigurationImpl():
41 dbType(DbType::UNKNOWN)
45 DatabaseConfigurationImpl::~DatabaseConfigurationImpl()
49 void DatabaseConfigurationImpl::checkAndApplyDbType(const std::string& type)
51 if (type == "redis-standalone")
52 dbType = DatabaseConfiguration::DbType::REDIS_STANDALONE;
53 else if (type == "redis-cluster")
54 dbType = DatabaseConfiguration::DbType::REDIS_CLUSTER;
55 else if (type == "redis-sentinel")
56 dbType = DatabaseConfiguration::DbType::REDIS_SENTINEL;
57 else if (type == "sdl-standalone-cluster")
58 dbType = DatabaseConfiguration::DbType::SDL_STANDALONE_CLUSTER;
59 else if (type == "sdl-sentinel-cluster")
60 dbType = DatabaseConfiguration::DbType::SDL_SENTINEL_CLUSTER;
62 throw DatabaseConfiguration::InvalidDbType(type);
65 DatabaseConfiguration::DbType DatabaseConfigurationImpl::getDbType() const
70 void DatabaseConfigurationImpl::checkAndApplyServerAddress(const std::string& address)
72 serverAddresses.push_back(HostAndPort(address, htons(DEFAULT_PORT)));
75 bool DatabaseConfigurationImpl::isEmpty() const
77 return serverAddresses.empty();
80 DatabaseConfiguration::Addresses DatabaseConfigurationImpl::getServerAddresses() const
82 return serverAddresses;
85 DatabaseConfiguration::Addresses DatabaseConfigurationImpl::getServerAddresses(const boost::optional<std::size_t>& addressIndex) const
88 return { HostAndPort(serverAddresses.at(*addressIndex)) };
90 return serverAddresses;
93 DatabaseConfiguration::Addresses DatabaseConfigurationImpl::getDefaultServerAddresses() const
95 return { HostAndPort(getDefaultHost(), htons(DEFAULT_PORT)) };
98 void DatabaseConfigurationImpl::checkAndApplySentinelAddress(const std::string& address)
100 sentinelAddress = HostAndPort(address, htons(DEFAULT_SENTINEL_PORT));
103 boost::optional<HostAndPort> DatabaseConfigurationImpl::getSentinelAddress() const
105 return sentinelAddress;
108 boost::optional<HostAndPort> DatabaseConfigurationImpl::getSentinelAddress(const boost::optional<std::size_t>& addressIndex) const
111 return { HostAndPort(serverAddresses.at(*addressIndex).getHost(), sentinelAddress->getPort()) };
113 return getSentinelAddress();
116 void DatabaseConfigurationImpl::checkAndApplySentinelMasterName(const std::string& name)
118 sentinelMasterName = name;
121 std::string DatabaseConfigurationImpl::getSentinelMasterName() const
123 return sentinelMasterName;