Rolf Badorek | ef2bf51 | 2019-08-20 11:17:15 +0300 | [diff] [blame] | 1 | /* |
Petri Ovaska | 5bdca62 | 2022-03-10 09:59:23 +0200 | [diff] [blame] | 2 | Copyright (c) 2018-2022 Nokia. |
Rolf Badorek | ef2bf51 | 2019-08-20 11:17:15 +0300 | [diff] [blame] | 3 | |
| 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 Tietavainen | a0745d2 | 2019-11-28 09:55:22 +0200 | [diff] [blame] | 17 | /* |
| 18 | * This source code is part of the near-RT RIC (RAN Intelligent Controller) |
| 19 | * platform project (RICP). |
| 20 | */ |
| 21 | |
Rolf Badorek | ef2bf51 | 2019-08-20 11:17:15 +0300 | [diff] [blame] | 22 | #include "private/databaseconfigurationimpl.hpp" |
| 23 | #include <arpa/inet.h> |
Petri Ovaska | ece6708 | 2021-04-15 11:08:13 +0300 | [diff] [blame] | 24 | #include <boost/crc.hpp> |
Petri Ovaska | 5bdca62 | 2022-03-10 09:59:23 +0200 | [diff] [blame] | 25 | #include <boost/lexical_cast.hpp> |
| 26 | #include <boost/algorithm/string.hpp> |
Rolf Badorek | ef2bf51 | 2019-08-20 11:17:15 +0300 | [diff] [blame] | 27 | |
| 28 | using namespace shareddatalayer; |
| 29 | |
| 30 | namespace |
| 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 Badorek | 2dcf940 | 2019-10-01 18:33:58 +0300 | [diff] [blame] | 39 | const uint16_t DEFAULT_SENTINEL_PORT(26379U); |
Petri Ovaska | 5bdca62 | 2022-03-10 09:59:23 +0200 | [diff] [blame] | 40 | const std::string DEFAULT_SENTINEL_MASTER_GROUP_NAME("dbaasmaster"); |
Rolf Badorek | ef2bf51 | 2019-08-20 11:17:15 +0300 | [diff] [blame] | 41 | } |
| 42 | |
| 43 | DatabaseConfigurationImpl::DatabaseConfigurationImpl(): |
| 44 | dbType(DbType::UNKNOWN) |
| 45 | { |
| 46 | } |
| 47 | |
| 48 | DatabaseConfigurationImpl::~DatabaseConfigurationImpl() |
| 49 | { |
| 50 | } |
| 51 | |
| 52 | void 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 Badorek | 2dcf940 | 2019-10-01 18:33:58 +0300 | [diff] [blame] | 58 | else if (type == "redis-sentinel") |
| 59 | dbType = DatabaseConfiguration::DbType::REDIS_SENTINEL; |
Petri Ovaska | 1c4a605 | 2021-05-31 14:06:32 +0300 | [diff] [blame] | 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; |
Rolf Badorek | ef2bf51 | 2019-08-20 11:17:15 +0300 | [diff] [blame] | 64 | else |
| 65 | throw DatabaseConfiguration::InvalidDbType(type); |
| 66 | } |
| 67 | |
| 68 | DatabaseConfiguration::DbType DatabaseConfigurationImpl::getDbType() const |
| 69 | { |
| 70 | return dbType; |
| 71 | } |
| 72 | |
| 73 | void DatabaseConfigurationImpl::checkAndApplyServerAddress(const std::string& address) |
| 74 | { |
| 75 | serverAddresses.push_back(HostAndPort(address, htons(DEFAULT_PORT))); |
| 76 | } |
| 77 | |
| 78 | bool DatabaseConfigurationImpl::isEmpty() const |
| 79 | { |
| 80 | return serverAddresses.empty(); |
| 81 | } |
| 82 | |
| 83 | DatabaseConfiguration::Addresses DatabaseConfigurationImpl::getServerAddresses() const |
| 84 | { |
| 85 | return serverAddresses; |
| 86 | } |
| 87 | |
Petri Ovaska | 1c4a605 | 2021-05-31 14:06:32 +0300 | [diff] [blame] | 88 | DatabaseConfiguration::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 Badorek | ef2bf51 | 2019-08-20 11:17:15 +0300 | [diff] [blame] | 96 | DatabaseConfiguration::Addresses DatabaseConfigurationImpl::getDefaultServerAddresses() const |
| 97 | { |
| 98 | return { HostAndPort(getDefaultHost(), htons(DEFAULT_PORT)) }; |
| 99 | } |
Rolf Badorek | 2dcf940 | 2019-10-01 18:33:58 +0300 | [diff] [blame] | 100 | |
Petri Ovaska | 5bdca62 | 2022-03-10 09:59:23 +0200 | [diff] [blame] | 101 | void DatabaseConfigurationImpl::checkAndApplySentinelPorts(const std::string& portsEnvStr) |
Rolf Badorek | 2dcf940 | 2019-10-01 18:33:58 +0300 | [diff] [blame] | 102 | { |
Petri Ovaska | 5bdca62 | 2022-03-10 09:59:23 +0200 | [diff] [blame] | 103 | std::vector<std::string> ports; |
| 104 | boost::split(ports, portsEnvStr, boost::is_any_of(",")); |
Rolf Badorek | 2dcf940 | 2019-10-01 18:33:58 +0300 | [diff] [blame] | 105 | |
Petri Ovaska | 5bdca62 | 2022-03-10 09:59:23 +0200 | [diff] [blame] | 106 | 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 Badorek | 2dcf940 | 2019-10-01 18:33:58 +0300 | [diff] [blame] | 115 | } |
| 116 | |
Petri Ovaska | ece6708 | 2021-04-15 11:08:13 +0300 | [diff] [blame] | 117 | boost::optional<HostAndPort> DatabaseConfigurationImpl::getSentinelAddress(const boost::optional<std::size_t>& addressIndex) const |
| 118 | { |
Petri Ovaska | 5bdca62 | 2022-03-10 09:59:23 +0200 | [diff] [blame] | 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)); |
Petri Ovaska | ece6708 | 2021-04-15 11:08:13 +0300 | [diff] [blame] | 121 | |
Petri Ovaska | 5bdca62 | 2022-03-10 09:59:23 +0200 | [diff] [blame] | 122 | if (!(serverAddresses.size() > 0)) |
| 123 | return {}; |
| 124 | |
| 125 | return { HostAndPort(serverAddresses.at(index).getHost(), port) }; |
Petri Ovaska | ece6708 | 2021-04-15 11:08:13 +0300 | [diff] [blame] | 126 | } |
| 127 | |
Petri Ovaska | 5bdca62 | 2022-03-10 09:59:23 +0200 | [diff] [blame] | 128 | void DatabaseConfigurationImpl::checkAndApplySentinelMasterNames(const std::string& sentinelMasterNamesEnvStr) |
Rolf Badorek | 2dcf940 | 2019-10-01 18:33:58 +0300 | [diff] [blame] | 129 | { |
Petri Ovaska | 5bdca62 | 2022-03-10 09:59:23 +0200 | [diff] [blame] | 130 | boost::split(sentinelMasterNames, sentinelMasterNamesEnvStr, boost::is_any_of(",")); |
Rolf Badorek | 2dcf940 | 2019-10-01 18:33:58 +0300 | [diff] [blame] | 131 | } |
| 132 | |
Petri Ovaska | 5bdca62 | 2022-03-10 09:59:23 +0200 | [diff] [blame] | 133 | std::string DatabaseConfigurationImpl::getSentinelMasterName(const boost::optional<std::size_t>& addressIndex) const |
Rolf Badorek | 2dcf940 | 2019-10-01 18:33:58 +0300 | [diff] [blame] | 134 | { |
Petri Ovaska | 5bdca62 | 2022-03-10 09:59:23 +0200 | [diff] [blame] | 135 | std::size_t index(addressIndex ? *addressIndex : 0); |
| 136 | return ((sentinelMasterNames.size() > 0 && index < sentinelMasterNames.size()) ? sentinelMasterNames.at(index) : DEFAULT_SENTINEL_MASTER_GROUP_NAME); |
Rolf Badorek | 2dcf940 | 2019-10-01 18:33:58 +0300 | [diff] [blame] | 137 | } |