blob: ac691ab4e77ef30227d9d4206b1ca772972c7d52 [file] [log] [blame]
Rolf Badorekef2bf512019-08-20 11:17:15 +03001/*
Petri Ovaska5bdca622022-03-10 09:59:23 +02002 Copyright (c) 2018-2022 Nokia.
Rolf Badorekef2bf512019-08-20 11:17:15 +03003
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
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
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.
15*/
16
Timo Tietavainena0745d22019-11-28 09:55:22 +020017/*
18 * This source code is part of the near-RT RIC (RAN Intelligent Controller)
19 * platform project (RICP).
20*/
21
Rolf Badorekef2bf512019-08-20 11:17:15 +030022#include "private/databaseconfigurationimpl.hpp"
23#include <arpa/inet.h>
Petri Ovaskaece67082021-04-15 11:08:13 +030024#include <boost/crc.hpp>
Petri Ovaska5bdca622022-03-10 09:59:23 +020025#include <boost/lexical_cast.hpp>
26#include <boost/algorithm/string.hpp>
Rolf Badorekef2bf512019-08-20 11:17:15 +030027
28using namespace shareddatalayer;
29
30namespace
31{
32 const std::string& getDefaultHost()
33 {
34 static const std::string defaultHost("localhost");
35 return defaultHost;
36 }
37
38 const uint16_t DEFAULT_PORT(6379U);
Rolf Badorek2dcf9402019-10-01 18:33:58 +030039 const uint16_t DEFAULT_SENTINEL_PORT(26379U);
Petri Ovaska5bdca622022-03-10 09:59:23 +020040 const std::string DEFAULT_SENTINEL_MASTER_GROUP_NAME("dbaasmaster");
Rolf Badorekef2bf512019-08-20 11:17:15 +030041}
42
43DatabaseConfigurationImpl::DatabaseConfigurationImpl():
44 dbType(DbType::UNKNOWN)
45{
46}
47
48DatabaseConfigurationImpl::~DatabaseConfigurationImpl()
49{
50}
51
52void DatabaseConfigurationImpl::checkAndApplyDbType(const std::string& type)
53{
54 if (type == "redis-standalone")
55 dbType = DatabaseConfiguration::DbType::REDIS_STANDALONE;
56 else if (type == "redis-cluster")
57 dbType = DatabaseConfiguration::DbType::REDIS_CLUSTER;
Rolf Badorek2dcf9402019-10-01 18:33:58 +030058 else if (type == "redis-sentinel")
59 dbType = DatabaseConfiguration::DbType::REDIS_SENTINEL;
Petri Ovaska1c4a6052021-05-31 14:06:32 +030060 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;
Rolf Badorekef2bf512019-08-20 11:17:15 +030064 else
65 throw DatabaseConfiguration::InvalidDbType(type);
66}
67
68DatabaseConfiguration::DbType DatabaseConfigurationImpl::getDbType() const
69{
70 return dbType;
71}
72
73void DatabaseConfigurationImpl::checkAndApplyServerAddress(const std::string& address)
74{
75 serverAddresses.push_back(HostAndPort(address, htons(DEFAULT_PORT)));
76}
77
78bool DatabaseConfigurationImpl::isEmpty() const
79{
80 return serverAddresses.empty();
81}
82
83DatabaseConfiguration::Addresses DatabaseConfigurationImpl::getServerAddresses() const
84{
85 return serverAddresses;
86}
87
Petri Ovaska1c4a6052021-05-31 14:06:32 +030088DatabaseConfiguration::Addresses DatabaseConfigurationImpl::getServerAddresses(const boost::optional<std::size_t>& addressIndex) const
89{
90 if (addressIndex)
91 return { HostAndPort(serverAddresses.at(*addressIndex)) };
92
93 return serverAddresses;
94}
95
Rolf Badorekef2bf512019-08-20 11:17:15 +030096DatabaseConfiguration::Addresses DatabaseConfigurationImpl::getDefaultServerAddresses() const
97{
98 return { HostAndPort(getDefaultHost(), htons(DEFAULT_PORT)) };
99}
Rolf Badorek2dcf9402019-10-01 18:33:58 +0300100
Petri Ovaska5bdca622022-03-10 09:59:23 +0200101void DatabaseConfigurationImpl::checkAndApplySentinelPorts(const std::string& portsEnvStr)
Rolf Badorek2dcf9402019-10-01 18:33:58 +0300102{
Petri Ovaska5bdca622022-03-10 09:59:23 +0200103 std::vector<std::string> ports;
104 boost::split(ports, portsEnvStr, boost::is_any_of(","));
Rolf Badorek2dcf9402019-10-01 18:33:58 +0300105
Petri Ovaska5bdca622022-03-10 09:59:23 +0200106 for (auto port : ports)
107 {
108 try {
109 sentinelPorts.push_back(htons(boost::lexical_cast<uint16_t>(port)));
110 }
111 catch (boost::bad_lexical_cast const &) {
112 continue;
113 }
114 }
Rolf Badorek2dcf9402019-10-01 18:33:58 +0300115}
116
Petri Ovaskaece67082021-04-15 11:08:13 +0300117boost::optional<HostAndPort> DatabaseConfigurationImpl::getSentinelAddress(const boost::optional<std::size_t>& addressIndex) const
118{
Petri Ovaska5bdca622022-03-10 09:59:23 +0200119 std::size_t index(addressIndex ? *addressIndex : 0);
120 uint16_t port((sentinelPorts.size() > 0 && index < sentinelPorts.size()) ? sentinelPorts.at(index) : htons(DEFAULT_SENTINEL_PORT));
Petri Ovaskaece67082021-04-15 11:08:13 +0300121
Petri Ovaska5bdca622022-03-10 09:59:23 +0200122 if (!(serverAddresses.size() > 0))
123 return {};
124
125 return { HostAndPort(serverAddresses.at(index).getHost(), port) };
Petri Ovaskaece67082021-04-15 11:08:13 +0300126}
127
Petri Ovaska5bdca622022-03-10 09:59:23 +0200128void DatabaseConfigurationImpl::checkAndApplySentinelMasterNames(const std::string& sentinelMasterNamesEnvStr)
Rolf Badorek2dcf9402019-10-01 18:33:58 +0300129{
Petri Ovaska5bdca622022-03-10 09:59:23 +0200130 boost::split(sentinelMasterNames, sentinelMasterNamesEnvStr, boost::is_any_of(","));
Rolf Badorek2dcf9402019-10-01 18:33:58 +0300131}
132
Petri Ovaska5bdca622022-03-10 09:59:23 +0200133std::string DatabaseConfigurationImpl::getSentinelMasterName(const boost::optional<std::size_t>& addressIndex) const
Rolf Badorek2dcf9402019-10-01 18:33:58 +0300134{
Petri Ovaska5bdca622022-03-10 09:59:23 +0200135 std::size_t index(addressIndex ? *addressIndex : 0);
136 return ((sentinelMasterNames.size() > 0 && index < sentinelMasterNames.size()) ? sentinelMasterNames.at(index) : DEFAULT_SENTINEL_MASTER_GROUP_NAME);
Rolf Badorek2dcf9402019-10-01 18:33:58 +0300137}