Rolf Badorek | ef2bf51 | 2019-08-20 11:17:15 +0300 | [diff] [blame] | 1 | /* |
| 2 | Copyright (c) 2018-2019 Nokia. |
| 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> |
| 24 | |
| 25 | using namespace shareddatalayer; |
| 26 | |
| 27 | namespace |
| 28 | { |
| 29 | const std::string& getDefaultHost() |
| 30 | { |
| 31 | static const std::string defaultHost("localhost"); |
| 32 | return defaultHost; |
| 33 | } |
| 34 | |
| 35 | const uint16_t DEFAULT_PORT(6379U); |
Rolf Badorek | 2dcf940 | 2019-10-01 18:33:58 +0300 | [diff] [blame] | 36 | const uint16_t DEFAULT_SENTINEL_PORT(26379U); |
Rolf Badorek | ef2bf51 | 2019-08-20 11:17:15 +0300 | [diff] [blame] | 37 | } |
| 38 | |
| 39 | DatabaseConfigurationImpl::DatabaseConfigurationImpl(): |
| 40 | dbType(DbType::UNKNOWN) |
| 41 | { |
| 42 | } |
| 43 | |
| 44 | DatabaseConfigurationImpl::~DatabaseConfigurationImpl() |
| 45 | { |
| 46 | } |
| 47 | |
| 48 | void DatabaseConfigurationImpl::checkAndApplyDbType(const std::string& type) |
| 49 | { |
| 50 | if (type == "redis-standalone") |
| 51 | dbType = DatabaseConfiguration::DbType::REDIS_STANDALONE; |
| 52 | else if (type == "redis-cluster") |
| 53 | dbType = DatabaseConfiguration::DbType::REDIS_CLUSTER; |
Rolf Badorek | 2dcf940 | 2019-10-01 18:33:58 +0300 | [diff] [blame] | 54 | else if (type == "redis-sentinel") |
| 55 | dbType = DatabaseConfiguration::DbType::REDIS_SENTINEL; |
Rolf Badorek | ef2bf51 | 2019-08-20 11:17:15 +0300 | [diff] [blame] | 56 | else |
| 57 | throw DatabaseConfiguration::InvalidDbType(type); |
| 58 | } |
| 59 | |
| 60 | DatabaseConfiguration::DbType DatabaseConfigurationImpl::getDbType() const |
| 61 | { |
| 62 | return dbType; |
| 63 | } |
| 64 | |
| 65 | void DatabaseConfigurationImpl::checkAndApplyServerAddress(const std::string& address) |
| 66 | { |
| 67 | serverAddresses.push_back(HostAndPort(address, htons(DEFAULT_PORT))); |
| 68 | } |
| 69 | |
| 70 | bool DatabaseConfigurationImpl::isEmpty() const |
| 71 | { |
| 72 | return serverAddresses.empty(); |
| 73 | } |
| 74 | |
| 75 | DatabaseConfiguration::Addresses DatabaseConfigurationImpl::getServerAddresses() const |
| 76 | { |
| 77 | return serverAddresses; |
| 78 | } |
| 79 | |
| 80 | DatabaseConfiguration::Addresses DatabaseConfigurationImpl::getDefaultServerAddresses() const |
| 81 | { |
| 82 | return { HostAndPort(getDefaultHost(), htons(DEFAULT_PORT)) }; |
| 83 | } |
Rolf Badorek | 2dcf940 | 2019-10-01 18:33:58 +0300 | [diff] [blame] | 84 | |
| 85 | void DatabaseConfigurationImpl::checkAndApplySentinelAddress(const std::string& address) |
| 86 | { |
| 87 | sentinelAddress = HostAndPort(address, htons(DEFAULT_SENTINEL_PORT)); |
| 88 | } |
| 89 | |
| 90 | boost::optional<HostAndPort> DatabaseConfigurationImpl::getSentinelAddress() const |
| 91 | { |
| 92 | return sentinelAddress; |
| 93 | } |
| 94 | |
| 95 | void DatabaseConfigurationImpl::checkAndApplySentinelMasterName(const std::string& name) |
| 96 | { |
| 97 | sentinelMasterName = name; |
| 98 | } |
| 99 | |
| 100 | std::string DatabaseConfigurationImpl::getSentinelMasterName() const |
| 101 | { |
| 102 | return sentinelMasterName; |
| 103 | } |